C#.net中的字符串问题 [英] string problem in C#.net
本文介绍了C#.net中的字符串问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
字符串str = 2 3 40 300 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 5 4 30 200 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 3 2 30 200
上面的str必须像这样显示
2 3 40 300
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
string str=2 3 40 300 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 5 4 30 200 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 3 2 30 200
the above str has to display like this
2 3 40 300
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
推荐答案
确定-如果您基于空格对它进行拆分,则可以轻松编写显示该代码并在每四个字符串中插入一个换行符的代码.
OK - if you split it based on a space, you can easily write code that displays it and inserts a line break every fourth string.
您在"3 2 30 20"之前错过了一个"0".我加了.现在的结果是完美的..
试试这个:
Hi,
You missed one "0" before "3 2 30 20". I added that. Now the result is perfect..
Try this:
private void GetFormat()
{
string str = "2 3 40 300 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 5 4 30 200 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 3 2 30 200";
string[] s4 = str.Split(' ');
int count = 0;
foreach (string s2 in s4)
{
if (count < 4)
{
Console.Write(s2 + "\t");
count++;
}
else
{
Console.WriteLine("");
count = 0;
}
}
}
让我知道它是否对您有用.
--Amit
Let me know if it works for you.
--Amit
int i=0;
foreach( var Parts in str.Split(' '))
{
Console.Out.writeLine(Parts+" ");
i++;
if(i==4)
{
Console.Out.WriteLine("");
i=0;
}
}
这篇关于C#.net中的字符串问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文