Created
January 5, 2014 15:52
-
-
Save ClausPolanka/8269860 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
class WrapperV2 | |
{ | |
public static string Wrap(string text, int zeilenlänge) | |
{ | |
return Wrap("", text, zeilenlänge); | |
} | |
static string Wrap(string umgebrochen, string text, int zeilenlänge) | |
{ | |
if (text == "") return umgebrochen; | |
var abbruch = Zeile_abbrechen(text, zeilenlänge); | |
abbruch = Abbruch_bei_Leerzeichen_präferieren(abbruch); | |
umgebrochen = Umbrechen(umgebrochen, abbruch.Zeile); | |
return Wrap(umgebrochen, abbruch.Rest, zeilenlänge); | |
} | |
static dynamic Zeile_abbrechen(string text, int zeilenlänge) | |
{ | |
dynamic abbruch = new ExpandoObject(); | |
abbruch.Zeile = text.Substring(0, text.Length < zeilenlänge | |
? text.Length | |
: zeilenlänge); | |
abbruch.Rest = zeilenlänge >= text.Length | |
? "" | |
: text.Substring(zeilenlänge); | |
return abbruch; | |
} | |
static dynamic Abbruch_bei_Leerzeichen_präferieren(dynamic abbruch) | |
{ | |
if (!abbruch.Zeile.EndsWith(" ") && !abbruch.Rest.StartsWith(" ")) | |
{ | |
var index_letztes_leerzeichen = abbruch.Zeile.LastIndexOf(" "); | |
if (index_letztes_leerzeichen > 0) | |
{ | |
var wortkopf = abbruch.Zeile.Substring(index_letztes_leerzeichen + 1); | |
abbruch.Zeile = abbruch.Zeile.Substring(0, index_letztes_leerzeichen); | |
abbruch.Rest = wortkopf + abbruch.Rest; | |
} | |
} | |
abbruch.Zeile = abbruch.Zeile.Trim(); | |
abbruch.Rest = abbruch.Rest.TrimStart(); | |
return abbruch; | |
} | |
static string Umbrechen(string umgebrochen, string zeile) | |
{ | |
return umgebrochen + (umgebrochen == "" ? "" : "\n") + zeile; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment