add Shorten() string extension method

This commit is contained in:
2021-02-08 13:58:33 +01:00
parent 39d3aa37b1
commit 6340459c55
3 changed files with 17 additions and 5 deletions
+16
View File
@@ -168,5 +168,21 @@ namespace zero.Core.Extensions
return newLineCharsRegex.Replace(input, String.Empty).Trim();
}
public static string Shorten(this string input, int maxLength)
{
if (maxLength < 4)
{
throw new ArgumentOutOfRangeException("maxLength", "Has to be at least 4 characters long");
}
if (String.IsNullOrEmpty(input) || input.Length <= maxLength)
{
return input;
}
return input.Substring(0, maxLength - 3) + "...";
}
}
}