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

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

    I found this question after searching for "C# truncate ellipsis". Using various answers, I created my own solution with the following features:

    1. An extension method
    2. Add an ellipsis
    3. Make the ellipsis optional
    4. Validate that the string is not null or empty before attempting to truncate it.

      public static class StringExtensions
      {
          public static string Truncate(this string value, 
              int maxLength, 
              bool addEllipsis = false)
          {
              // Check for valid string before attempting to truncate
              if (string.IsNullOrEmpty(value)) return value;
      
              // Proceed with truncating
              var result = string.Empty;
              if (value.Length > maxLength)
              {
                  result = value.Substring(0, maxLength);
                  if (addEllipsis) result += "...";
              }
              else
              {
                  result = value;
              }
      
              return result;
          }
      }
      

    I hope this helps someone else.

    0 讨论(0)
  • 2020-12-13 23:40

    There isn't a built in method in the .NET Framework which does this, however this is a very easy method to write yourself. Here are the steps, try making it yourself and let us know what you come up with.

    1. Create a method, perhaps an extension method public static void TruncateWithEllipsis(this string value, int maxLength)

    2. Check to see if the passed in value is greater than the maxLength specified using the Length property. If the value not greater than maxLength, just return the value.

    3. If we didn't return the passed in value as is, then we know we need to truncate. So we need to get a smaller section of the string using the SubString method. That method will return a smaller section of a string based on a specified start and end value. The end position is what was passed in by the maxLength parameter, so use that.

    4. Return the sub section of the string plus the ellipsis.

    A great exercise for later would be to update the method and have it break only after full words. You can also create an overload to specify how you would like to show a string has been truncated. For example, the method could return " (click for more)" instead of "..." if your application is set up to show more detail by clicking.

    0 讨论(0)
  • 2020-12-13 23:41
    string s = "abcdefg";
    if (s.length > 3)
    {
        s = s.substring(0,3);
    }
    

    You can use the Substring function.

    0 讨论(0)
  • 2020-12-13 23:41

    Sure, here is some sample code:

    string str = "abcdefg";
    if (str.Length > X){
       str = str.Substring(0, X) + "...";
    }
    
    0 讨论(0)
  • 2020-12-13 23:45
    public string TruncString(string myStr, int THRESHOLD)
    {
        if (myStr.Length > THRESHOLD)
            return myStr.Substring(0, THRESHOLD) + "...";
        return myStr;
    }
    

    Ignore the naming convention it's just in case he actually needs the THRESHOLD variable or if it's always the same size.

    Alternatively

    string res = (myStr.Length > THRESHOLD) ? myStr.Substring(0, THRESHOLD) + ".." : myStr;
    
    0 讨论(0)
提交回复
热议问题