Created
August 12, 2013 03:28
-
-
Save camt/6208072 to your computer and use it in GitHub Desktop.
Break a string down into a paragraph based on last occurrence of a space.
Set up to do this for webpage ("<br />"), however easy enough to replace the break with whatever is required for your needs.
Not the most elegant, but it works happily.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private string FormatStringBreaks(string data, int maxLength) | |
{ | |
string res = ""; | |
while (!string.IsNullOrEmpty(data)) | |
{ | |
var dLength = data.Length; | |
if (dLength > maxLength) | |
{ | |
var next = data.Substring(0, maxLength); | |
var last = next.LastIndexOf(" "); | |
res = res + data.Substring(0, last) + "<br />"; // System.Environment.NewLine, \n, \r\n, etc | |
data = data.Substring(last + 1); | |
} | |
else | |
{ | |
res = res + data; | |
data = null; | |
} | |
} | |
return res; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment