我有一个下面提到的字符串,我正在从该字符串中提取2D数组(我正在成功完成此操作),问题是我不知道如何退出while循环。
00000000,00000000,10100111,10100101,11111101,11111111,00000111,00000000,
00000000,00000000,11100010,00100010,11111111,11111111,00000000,10000000,
00000000,00000000,00001001,00001000,00111111,00111111,00000000,00000000,
00100000,00100000,10101110,10100010,10111110,10111110,00000000,00001000,
00000000,00111000,00100011,01111110,01111111,01000011,01000000,01111000,
00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,
00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,
我试图将“ x”放在字符串的末尾并像这样运行while循环,它可以工作,但是对我而言这是不可行的,因为我必须从数百个文件中读取此字符串。
while (Data_string[counter] != 'x' )
{
while (Data_string[counter] != ',')
{
if (Data_string[counter] != '\n')
SubString += Data_string[counter];
counter++;
}
// here I use Substring to covert to int and then set Substring to empty string
}
所以我正在寻找一种有效的解决方案,所以我不必更改字符串,
谢谢..
答案 0 :(得分:4)
只需使用
while (counter < Data_string.Length)
{
}
答案 1 :(得分:2)
这是我的处理方式:
class Program
{
static void Main(string[] args)
{
string str = "00000000,00000000,10100111,10100101,11111101,11111111,00000111,00000000, 00000000,00000000,11100010,00100010,11111111,11111111,00000000,10000000, 00000000,00000000,00001001,00001000,00111111,00111111,00000000,00000000, 00100000,00100000,10101110,10100010,10111110,10111110,00000000,00001000, 00000000,00111000,00100011,01111110,01111111,01000011,01000000,01111000, 00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000, 00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,";
string[] codes = str.Split(',');
}
}
您根本不需要while循环。
如果要处理空白数据的情况,可以采用以下方法:
class Program
{
static void Main(string[] args)
{
string str = "00000000,00000000,10100111,10100101,11111101,11111111,00000111,00000000, 00000000,00000000,11100010,00100010,11111111,11111111,00000000,10000000, 00000000,00000000,00001001,00001000,00111111,00111111,00000000,00000000, 00100000,00100000,10101110,10100010,10111110,10111110,00000000,00001000, 00000000,00111000,00100011,01111110,01111111,01000011,01000000,01111000, 00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000, 00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,";
string[] codes = str.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
}
}
class Program
{
static void Main(string[] args)
{
string str = "00000000,00000000,10100111,10100101,11111101,11111111,00000111,00000000, 00000000,00000000,11100010,00100010,11111111,11111111,00000000,10000000, 00000000,00000000,00001001,00001000,00111111,00111111,00000000,00000000, 00100000,00100000,10101110,10100010,10111110,10111110,00000000,00001000, 00000000,00111000,00100011,01111110,01111111,01000011,01000000,01111000, 00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000, 00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,";
string[] codes = str.Split(',').Where(x => string.IsNullOrEmpty(x) == false).ToArray();
}
}
class Program
{
static void Main(string[] args)
{
string str = "00000000,00000000,10100111,10100101,11111101,11111111,00000111,00000000, 00000000,00000000,11100010,00100010,11111111,11111111,00000000,10000000, 00000000,00000000,00001001,00001000,00111111,00111111,00000000,00000000, 00100000,00100000,10101110,10100010,10111110,10111110,00000000,00001000, 00000000,00111000,00100011,01111110,01111111,01000011,01000000,01111000, 00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000, 00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,";
List<string> codes = str.Split(',').ToList();
codes.RemoveAll(x => string.IsNullOrEmpty(x));
}
}
答案 2 :(得分:2)
如果只想遍历每个二进制记录,则无需将字符串转换为2D数组并在字符串末尾添加X
。
您可以通过,
轻松拆分数组,并遍历数组的每个元素,例如
string input ="00000000,00000000,10100111,10100101,11111101,11111111,00000111,00000000, 00000000,00000000,11100010,00100010,11111111,11111111,00000000,10000000, 00000000,00000000,00001001,00001000,00111111,00111111,00000000,00000000, 00100000,00100000,10101110,10100010,10111110,10111110,00000000,00001000, 00000000,00111000,00100011,01111110,01111111,01000011,01000000,01111000, 00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000, 00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000";
string inputArr = input.TrimEnd(',').split(',');
foreach(string item in inputArr)
{
Console.WriteLine(item);
}
从更新后的代码中,您似乎要删除所有,
并将所有数组元素连接到单个字符串,即SubString
。如果您要尝试这样做,则可以单行实现
string SubString = String.Join("",input.Split(','));
无需使用while/for loop
和/或if循环,这是一种清晰易读的方法。
POC:.net fiddle
答案 3 :(得分:1)
只需在循环中添加一个条件:
while (counter < Data_string.Length && Data_string[counter] != 'x' )
{
//some code
}
当然,您可以使用for
循环:
for(var i = 0; i < Data_string.Length && Data_string[i] != x; i++)
{
// some code
}
答案 4 :(得分:1)
曾经尝试过foreach
吗?只要继承了IEnumerable
接口,它就会根据集合的大小动态循环。
答案 5 :(得分:1)
使用string.Length
:
while (counter < Data_string.Length)
{ } // some code
侧面说明:C#中的通用代码样式避免了snake_case。对属性使用UpperCamelCase,对局部或私有变量使用lowerCamelCase。
答案 6 :(得分:0)
您可以使用文件长度作为停止条件