Skip to content

Instantly share code, notes, and snippets.

@camt
Created August 12, 2013 03:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save camt/6208072 to your computer and use it in GitHub Desktop.
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.
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