Skip to content

Instantly share code, notes, and snippets.

@ClausPolanka
Created January 5, 2014 15:46
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 ClausPolanka/8269802 to your computer and use it in GitHub Desktop.
Save ClausPolanka/8269802 to your computer and use it in GitHub Desktop.
public class Wrapper
{
public static string Wrap(string text, int zeilenlänge)
{
return Wrap("", text, zeilenlänge);
}
private static string Wrap(string umgebrochen, string text, int zeilenlänge)
{
if (text == "") return umgebrochen;
var zeile = text.Substring(0, Math.Min(text.Length, zeilenlänge));
var rest = zeilenlänge >= text.Length
? ""
: text.Substring(zeilenlänge, text.Length - zeile.Length);
if (!zeile.EndsWith(" ") && !rest.StartsWith(" "))
{
var index_of_last_whitespace = zeile.LastIndexOf(" ");
if (index_of_last_whitespace > 0)
{
var wortkopf = zeile.Substring(index_of_last_whitespace + 1);
zeile = zeile.Substring(0, index_of_last_whitespace);
rest = wortkopf + rest;
}
}
zeile = zeile.Trim();
rest = rest.Trim();
umgebrochen += (umgebrochen == "" ? "" : "\n") + zeile;
return Wrap(umgebrochen, rest, zeilenlänge);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment