Skip to content

Instantly share code, notes, and snippets.

@ClausPolanka
Created January 5, 2014 15:52
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/8269860 to your computer and use it in GitHub Desktop.
Save ClausPolanka/8269860 to your computer and use it in GitHub Desktop.
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