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

后端 未结 12 1004
醉话见心
醉话见心 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:15

    I added this in my project just because where I'm using it is a high chance of it being used in loops, in a project hosted online hence I didn't want any crashes if I could manage it. The length fits a column I have. It's C#7

    Just a one line:

     public static string SubStringN(this string Message, int Len = 499) => !String.IsNullOrEmpty(Message) ? (Message.Length >= Len ? Message.Substring(0, Len) : Message) : "";
    

提交回复
热议问题