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

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

    Maybe it is better to implement a method for that purpose:

    string shorten(sting yourStr)
    {
    //Suppose you have a string yourStr, toView and a constant value 
    
        string toView;
        const int maxView = 3;
    
        if (yourStr.Length > maxView)
            toView = yourStr.Substring(0, maxView) + " ..."; // all you have is to use Substring(int, int) .net method
        else
            toView = yourStr;
    return toView;
    }
    
    0 讨论(0)
  • 2020-12-13 23:24

    Here is the logic wrapped up in an extension method:

    public static string Truncate(this string value, int maxChars)
    {
        return value.Length <= maxChars ? value : value.Substring(0, maxChars) + "...";
    }
    

    Usage:

    var s = "abcdefg";
    
    Console.WriteLine(s.Truncate(3));
    
    0 讨论(0)
  • 2020-12-13 23:26

    All very good answers, but to clean it up just a little, if your strings are sentences, don't break your string in the middle of a word.

    private string TruncateForDisplay(this string value, int length)
    {
      if (string.IsNullOrEmpty(value)) return string.Empty;
      var returnValue = value;
      if (value.Length > length)
      {
        var tmp = value.Substring(0, length) ;
        if (tmp.LastIndexOf(' ') > 0)
           returnValue = tmp.Substring(0, tmp.LastIndexOf(' ') ) + " ...";
      }                
      return returnValue;
    }
    
    0 讨论(0)
  • 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:

    <td><%= shorten(YOUR_STRING_HERE) %></td>
    
    0 讨论(0)
  • 2020-12-13 23:33

    Here's a version that accounts for the length of the ellipses:

        public static string Truncate(this string value, int maxChars)
        {
            const string ellipses = "...";
            return value.Length <= maxChars ? value : value.Substring(0, maxChars - ellipses.Length) + ellipses;
        }
    
    0 讨论(0)
  • 2020-12-13 23:34

    I has this problem recently. I was storing a "status" message in a nvarcharMAX DB field which is 4000 characters. However my status messages were building up and hitting the exception.

    But it wasn't a simple case of truncation as an arbitrary truncation would orphan part of a status message, so I really needed to "truncate" at a consistent part of the string.

    I solved the problem by converting the string to a string array, removing the first element and then restoring to a string. Here is the code ("CurrentStatus" is the string holding the data)...

            if (CurrentStatus.Length >= 3750)
            {
                // perform some truncation to free up some space.
    
                // Lets get the status messages into an array for processing...
                // We use the period as the delimiter, then skip the first item and re-insert into an array.
    
                string[] statusArray = CurrentStatus.Split(new string[] { "." }, StringSplitOptions.None)
                                        .Skip(1).ToArray();
    
                // Next we return the data to a string and replace any escaped returns with proper one.
                CurrentStatus = (string.Join(".", statusArray))
                                        .Replace("\\r\\n", Environment.NewLine);
    
    
            }
    

    Hope it helps someone out.

    0 讨论(0)
提交回复
热议问题