如何使用c#打破字符串 [英] How to break a string using c#
问题描述
如何打破c中的字符串
Ex:
Cananyoneshowsmehowtoaddthelinebreak
预期:
Cana
nyon
esho
我试过了,\ n,\ r \\ n,但是没有工作。
我尝试了什么:
我试过了,\ n,\ r \\ n,但是没有工作。
Cana < br> nyon
换行有意义吗?
字符串没有中断,换行只是一个字符或字符序列,就字符串而言。只有当一个字符串输出到浏览器,显示器或打印机时,换行才变得相关 - 然后它取决于你执行换行的环境。
例如,输出到console:
Console.WriteLine( Cana< /跨度>);
Console.WriteLine( nyon);
和
Console.WriteLine( Cana\\\
nyon);
是equivelant,并且有两个换行符。
但是对于浏览器,你需要HTML:
Response.Write( Cana< br /> nyon< br /> );
是否需要。
对于打印或通过Paint事件输出到显示器,它变得更加复杂。
基本上,一个字符串不是一个可输出的对象,它没有换行符的概念 - 所以我们不能给出一刀切的答案。
使用 Environment.NewLine Property 是这样的:
string str = Cana + Environment.NewLine + nyon + Environment.NewLine + 无论;
是否要分成四个字符的块?
怎么样?string s = Cananyoneshowsmehowtoaddthelinebreak;
while (s.Length > 4 )
{
Console.WriteLine(s.Substring( 0 , 4 < /跨度>));
s = s.Substring( 4 );
}
如果(s.Length > 0 )
{
Console.WriteLine(s);
}干杯
Andi
How to break the string in c#
Ex:
Cananyoneshowsmehowtoaddthelinebreak
Expecting :
Cana
nyon
esho
I tried
,\n,\r\n, but not working.
What I have tried:
I tried
,\n,\r\n, but not working.
Cana
nyon
esho
wsme
What do you mean by "break a string"?
And where are newlines significant?
Strings don't have "breaks" and a newline is just a character or character sequence as far as a string is concerned. It's only when a string is output to a browser, display, or printer that line breaks become relevant - and then it depends on the environment what you do to perform a newline.
For example, outputting to a console:
Console.WriteLine("Cana"); Console.WriteLine("nyon");and
Console.WriteLine("Cana\nnyon");are equivelant, and put in two newlines.
But for a browser, you need HTML:
Response.Write("Cana<br />nyon<br />");Is needed.
For printing, or output to displays via Paint events, it gets even more complex.
Basically, a string isn't an "output-able" object, and it has no concept of a line break - so there is no "one size fits all" answer we can give.
Use the Environment.NewLine Property like this:
string str = "Cana" + Environment.NewLine + "nyon" + Environment.NewLine + "whatever";
Is it about splitting into chunks of four characters?
How about this?string s = "Cananyoneshowsmehowtoaddthelinebreak"; while(s.Length > 4) { Console.WriteLine(s.Substring(0,4)); s = s.Substring(4); } if (s.Length > 0) { Console.WriteLine(s); }Cheers
Andi
这篇关于如何使用c#打破字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!