use*_*047 4 c# line-breaks windows-phone-7
我会尽力解释我正在寻找的东西.目前,我正在使用此代码来换行每x个字符.
public static string SpliceText(string text, int lineLength)
{
return Regex.Replace(text, "(.{" + lineLength + "})", "$1" + Environment.NewLine);
}
Run Code Online (Sandbox Code Playgroud)
这很有效,但通常它会打破每个x数字,显然有时会突破一个单词.代码是否有可能检查它是否打破了中间单词,如果它不是中间单词可以打破,但现在检查中断后的第一个字符是否为空格并删除它,如果是这样的话?
我知道我要求很多,但无论如何都要提前感谢!
lex*_*Roy 13
试试这个:
public static string SpliceText(string text, int lineLength)
{
var charCount = 0;
var lines = text.Split(new string[] {" "}, StringSplitOptions.RemoveEmptyEntries)
.GroupBy(w => (charCount += w.Length + 1) / lineLength)
.Select(g => string.Join(" ", g));
return String.Join("\n", lines.ToArray());
}
Run Code Online (Sandbox Code Playgroud)
这是我的屏幕截图:

| 归档时间: |
|
| 查看次数: |
3143 次 |
| 最近记录: |