Problem with Substring() - ArgumentOutOfRangeException

后端 未结 11 1657
抹茶落季
抹茶落季 2020-12-20 11:29

I have a repeater that displays data from my Projects table. There are projectId, name and description. I use Substring(1, 240) on description. But sometimes the string is s

11条回答
  •  时光说笑
    2020-12-20 12:19

    An extension method:

    public static string SafeSubstring(this string text, int start, int length)
    {
       if (start >= text.Length)
          return "";            
       if (start + length > text.Length)
          length = text.Length - start;         
       return text.Substring(start, length);
    }
    

提交回复
热议问题