Tiny way to get the first 25 characters

后端 未结 12 1515
感动是毒
感动是毒 2020-12-29 13:12

Can anyone think of a nicer way to do the following:

public string ShortDescription
{
    get { return this.Description.Length <= 25 ? this.Description :          


        
12条回答
  •  时光说笑
    2020-12-29 13:54

    I'd stick with what you have tbh, but just as an alternative, if you have LINQ to objects you could

    new string(this.Description.ToCharArray().Take(25).ToArray())
    
    //And to maintain the ...
    + (this.Description.Length <= 25 ? String.Empty : "...")
    

    As others have said, you'd likely want to store 25 in a constant

提交回复
热议问题