.net 中是否有任何方法可以包装每行给定最大长度的短语?
例子:
Phrase: The quick red fox jumps over the lazy cat
Length: 20
结果:
The quick red fox
jumps over the lazy
cat
最佳答案
没有内置的方法。您可以使用正则表达式:
string text = "The quick brown fox jumps over the lazy dog.";
int minLength = 1;
int maxLength = 20;
MatchCollection lines = Regex.Matches(text, "(.{"+minLength.ToString()+","+maxLength.ToString()+"})(?: |$)|([^ ]{"+maxLength.ToString()+"})");
StringBuilder builder = new StringBuilder();
foreach (Match line in lines) builder.AppendLine(line.Value);
text = builder.ToString();
注意:我更正了the pangram .
关于c# - 在 .NET 中换行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1482300/