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

前端 未结 11 689
陌清茗
陌清茗 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:28

    Code behind:

    string shorten(sting s)
    {
        //string s = abcdefg;
        int tooLongInt = 3;
    
        if (s.Length > tooLongInt)
            return s.Substring(0, tooLongInt) + "..";
    
        return s;
    }
    

    Markup:

    <%= shorten(YOUR_STRING_HERE) %>
    

提交回复
热议问题