public static string ShortenPath(this string This, int maxLength, eStringShortening shortenWhere, string removedCharactersIndicator) { return(StringXT.StringShortenerWorkhorse(This, maxLength, true, shortenWhere, removedCharactersIndicator)); }
public static string ShortenPath(this string This, int maxLength, eStringShortening shortenWhere) { return(StringXT.StringShortenerWorkhorse(This, maxLength, true, shortenWhere, "...")); }
public static string Shorten(this string source, int maxLength, eStringShortening shortenWhere, string removedCharactersIndicator) { return(StringShortenerWorkhorse(source, maxLength, false, shortenWhere, removedCharactersIndicator)); }
internal static string StringShortenerWorkhorse(this string source, int maxLength, bool isPath, eStringShortening shortenWhere, string removedCharactersIndicator) { // Only shorten if it's worthwhile if (source.Length <= maxLength + removedCharactersIndicator.Length) { return(source); } switch (shortenWhere) { case eStringShortening.TakeFromMiddle: { if (isPath) { string sFilePart = string.Format("{0}\\{1}", removedCharactersIndicator, System.IO.Path.GetFileName(source)); string sBaseDir = System.IO.Path.GetDirectoryName(source); return(sBaseDir.Substring(0, maxLength - sFilePart.Length) + sFilePart); } else { int halfIndicatorLength = removedCharactersIndicator.Length / 2; int leftMiddle = (maxLength / 2) - halfIndicatorLength; int rightMiddle = (maxLength / 2) + (removedCharactersIndicator.Length - halfIndicatorLength); return(string.Format("{0}{1}{2}", source.Substring(0, leftMiddle), removedCharactersIndicator, source.Substring(rightMiddle))); } } case eStringShortening.TakeFromEnd: { return(string.Format("{0}{1}", source.Substring(0, maxLength - removedCharactersIndicator.Length), removedCharactersIndicator)); } default: return(source.Substring(0, maxLength)); } }
public static string Shorten(this string source, int maxLength, eStringShortening shortenWhere) { return(StringShortenerWorkhorse(source, maxLength, false, shortenWhere, "...")); }