I'll try to explain what I am looking for as best as I can. Currently, I am using this code to line break every x number of characters.
public static string SpliceText(string text, int lineLength)
{
return Regex.Replace(text, "(.{" + lineLength + "})", "$1" + Environment.NewLine);
}
This works great, but often it will break every x number and obviously will sometimes break through a word. Is it possible for code to check if it is breaking mid-word, and if it's not mid-word to break anyways but now check if the first character after the break is a space and remove it if so?
I know I am asking for a lot, but thanks in advance anyways!
Try this:
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());
}
Here's my screen shot for this:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With