C# 循环遍历行中包含未知值编号的列表
我有这个字符串:C# 循环遍历行中包含未知值编号的列表,c#,.net,arrays,regex,multidimensional-array,C#,.net,Arrays,Regex,Multidimensional Array,我有这个字符串: tokens3="1,2,3****5,8,9,1,2,3****1,2,3,4,5,6,7,8,9 string[] tokens4 = tokens3.Split(new string[] { "****" }, StringSplitOptions.None); 所以我想这会给我一个这样的列表: 1,2,3 5,8,9,1,2,3 1,2,3,4,5,6,7,8,9 我想循环遍历该列表并获得每个值。我该怎么做? 简而言之: 1-如何计算令牌4行?? 2-如何循环遍历每行
tokens3="1,2,3****5,8,9,1,2,3****1,2,3,4,5,6,7,8,9
string[] tokens4 = tokens3.Split(new string[] { "****" }, StringSplitOptions.None);
所以我想这会给我一个这样的列表:
1,2,3
5,8,9,1,2,3
1,2,3,4,5,6,7,8,9
我想循环遍历该列表并获得每个值。我该怎么做?
简而言之:
1-如何计算令牌4行??
2-如何循环遍历每行的值(我不知道一行有多少个值)?您可以通过与任何数组长度相同的方式获得
标记4的长度:标记4.length
您可以使用标准的foreach
循环通过tokens4
。如果需要的话,您可以从中以逗号分割每个标记列表
foreach (string tokenList in tokens4)
{
string[] tokens = tokenList.Split(',');
// tokens.Length gives the number of tokens
// You can loop through tokens using another loop
}
您可以使用与任何数组长度相同的方法获取tokens4的长度:tokens4.length
您可以使用标准的foreach
循环通过tokens4
。如果需要的话,您可以从中以逗号分割每个标记列表
foreach (string tokenList in tokens4)
{
string[] tokens = tokenList.Split(',');
// tokens.Length gives the number of tokens
// You can loop through tokens using another loop
}
您必须在嵌套的foreach中进行双重拆分
var tokens3 = "1,2,3****5,8,9,1,2,3****1,2,3,4,5,6,7,8,9";
string[] tokens4 = tokens3.Split(new string[] { "****" }, StringSplitOptions.None);
foreach(string token in tokens4)
{
var rows = token.Split(new string[] { "," }, StringSplitOptions.None);
foreach(var row in rows)
{
// Here you have an access to the row value
Console.WriteLine(row);
}
Console.WriteLine();
}
您必须在嵌套的foreach中进行双重拆分
var tokens3 = "1,2,3****5,8,9,1,2,3****1,2,3,4,5,6,7,8,9";
string[] tokens4 = tokens3.Split(new string[] { "****" }, StringSplitOptions.None);
foreach(string token in tokens4)
{
var rows = token.Split(new string[] { "," }, StringSplitOptions.None);
foreach(var row in rows)
{
// Here you have an access to the row value
Console.WriteLine(row);
}
Console.WriteLine();
}
试试这个
tokens3="1,2,3****5,8,9,1,2,3****1,2,3,4,5,6,7,8,9
string[] tokens4 = tokens3.Split(new string[] { "****" }, StringSplitOptions.None);
foreach(string SplitedToken in tokens4)
{
string[] Numbers = SplitedToken.Split(',');
}
试试这个
tokens3="1,2,3****5,8,9,1,2,3****1,2,3,4,5,6,7,8,9
string[] tokens4 = tokens3.Split(new string[] { "****" }, StringSplitOptions.None);
foreach(string SplitedToken in tokens4)
{
string[] Numbers = SplitedToken.Split(',');
}
试试这个正则表达式:
[\*]+(\d*?)
带有ExplicitCapture
标志。看
试试这个正则表达式:
[\*]+(\d*?)
带有ExplicitCapture
标志。看