我想在我的字符串中的9个单词之后插入一个换行符(\n),以便第9个单词后面的字符串在下一行.
string newline ="如何在(这里)字符串的第九个字后插入换行符,以便剩下的字符串在下一行中"
被困在这里:
foreach (char x in newline)
{
if (space < 8)
{
if (x == ' ')
{
space++;
}
}
}
Run Code Online (Sandbox Code Playgroud)
不知道为什么我被卡住了.我知道这很简单.
如果可能,请显示任何其他简单方法.
谢谢!
注意:找到自己的答案.由我在下面给出.
Tim*_*ter 12
对于它的价值,这是一个LINQ单线程:
string newline = "How to insert newline character after ninth word of(here) the string such that the remaining string is in next line";
string lines = string.Join(Environment.NewLine, newline.Split()
.Select((word, index) => new { word, index})
.GroupBy(x => x.index / 9)
.Select(grp => string.Join(" ", grp.Select(x=> x.word))));
Run Code Online (Sandbox Code Playgroud)
结果:
How to insert newline character after ninth word of(here)
the string such that the remaining string is in
next line
Run Code Online (Sandbox Code Playgroud)