Tiny way to safe substring

Problem:

public string ShortDescription
{
     get { return this.Description.Length <= 25 ? this.Description : this.Description.Substring(0, 25) + "..."; }
}

I would have liked to just do string.Substring(0, 25) but it throws an exception if the string is less than the length supplied.

Solution:

public static class StringExtensions
{
    public static string SafeSubstring(this string input, int startIndex, int length, string suffix)
    {
        if (input.Length >= (startIndex + length))
        {
            if (suffix == null) suffix = string.Empty;
            return input.Substring(startIndex, length) + suffix;
        }
        else
        {
            if (input.Length > startIndex)
            {
                return input.Substring(startIndex);
            }
            else
            {
                return string.Empty;
            }
        }
    }
}

Mirror from

StackOverflow - c# - Tiny way to get the first 25 characters - https://stackoverflow.com/questions/595004/tiny-way-to-get-the-first-25-characters

Previous Post Next Post