using System.Text.RegularExpressions;
private List<string> SplitPreserveWord(string sentence, int splitLength = 132)
{
string[] words = sentence.Split(' ');
var parts = new List<string>();
string part = string.Empty;
foreach (var word in words)
{
if (part.Length + GetSpecialCharCount(part) + word.Length + GetSpecialCharCount(word) < splitLength)
{
part += string.IsNullOrEmpty(part) ? word : " " + word;
}
else
{
parts.Add(part);
part = word;
}
}
parts.Add(part);
return parts;
}
private int GetSpecialCharCount(string word)
{
return Regex.Matches(word, @"\n").Count + Regex.Matches(word, @"\r").Count + Regex.Matches(word, @"\t").Count;
}
hi
ReplyDelete