How can I truncate my strings with a “…” if they are too long?

前端 未结 11 691
陌清茗
陌清茗 2020-12-13 23:13

Hope somebody has a good idea. I have strings like this:

abcdefg
abcde
abc

What I need is for them to be trucated to show like this if more

11条回答
  •  余生分开走
    2020-12-13 23:41

    Sure, here is some sample code:

    string str = "abcdefg";
    if (str.Length > X){
       str = str.Substring(0, X) + "...";
    }
    

提交回复
热议问题