What method in the String class returns only the first N characters?

后端 未结 12 977
醉话见心
醉话见心 2020-11-27 12:27

I\'d like to write an extension method to the String class so that if the input string to is longer than the provided length N, only the first

12条回答
  •  时光取名叫无心
    2020-11-27 13:07

    Simply:

    public static String Truncate(String input,int maxLength)
    {
       if(input.Length > maxLength)
          return input.Substring(0,maxLength);
       return input;
    }
    

提交回复
热议问题