namespace Kreta.BusinessLogic.Classes { public static class SubstringExtension { /// /// Sortörés kezelése /// public enum StringWrapMode { Cut = 0, Wrap = 1, TruncateTail = 2 } /// /// Abban az esetben, ha a startindex nagyobb mint 0, levágja a szöveg elejét /// /// /// /// /// public static string Wrap(this string text, int startindex, StringWrapMode wrapping = StringWrapMode.Cut) { return Wrap(text, startindex, text.Length, wrapping); } public static string Wrap(this string text, int startindex, int maxlength, StringWrapMode wrapping = StringWrapMode.Cut) { if (string.IsNullOrWhiteSpace(text)) { return ""; } if (startindex < 0 || (startindex == 0 && maxlength == text.Length)) { return text; } int maxIndex = text.Length - 1; int lastIndex = startindex + maxlength; if (lastIndex > maxIndex) { return text.Substring(startindex, maxIndex - startindex); } switch (wrapping) { case StringWrapMode.TruncateTail: return string.Format("{0}...", text.Substring(startindex, maxlength - 3)); case StringWrapMode.Wrap: int lastSpaceBeforeCut = -1; int lastSpaceAfterCut = -1; for (int i = 0; i < maxIndex; i++) { if (text[i] == ' ') { if (i > maxlength) { lastSpaceAfterCut = i; } else { lastSpaceBeforeCut = i; } } } return lastSpaceBeforeCut == -1 || lastSpaceAfterCut == -1 ? string.Format("{0}...", text.Substring(startindex, maxlength - 3)) : text.Substring(startindex, lastSpaceBeforeCut); default: return text.Substring(startindex, maxlength); } } } }