Created
January 5, 2014 15:46
-
-
Save ClausPolanka/8269802 to your computer and use it in GitHub Desktop.
This file contains 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
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