Truncate string on whole words in .NET C#

前端 未结 10 1245
走了就别回头了
走了就别回头了 2020-11-29 22:43

I am trying to truncate some long text in C#, but I don\'t want my string to be cut off part way through a word. Does anyone have a function that I can use to truncate my st

相关标签:
10条回答
  • 2020-11-29 23:15

    If you are using windows forms, in the Graphics.DrawString method, there is an option in StringFormat to specify if the string should be truncated, if it does not fit into the area specified. This will handle adding the ellipsis as necessary.

    http://msdn.microsoft.com/en-us/library/system.drawing.stringtrimming.aspx

    0 讨论(0)
  • 2020-11-29 23:15

    I use this

    public string Truncate(string content, int length)
        {
            try
            {
                return content.Substring(0,content.IndexOf(" ",length)) + "...";
            }
            catch
            {
                return content;
            }
        }
    
    0 讨论(0)
  • 2020-11-29 23:16

    Thanks for your answer Dave. I've tweaked the function a bit and this is what I'm using ... unless there are any more comments ;)

    public static string TruncateAtWord(this string input, int length)
    {
        if (input == null || input.Length < length)
            return input;
        int iNextSpace = input.LastIndexOf(" ", length, StringComparison.Ordinal);
        return string.Format("{0}…", input.Substring(0, (iNextSpace > 0) ? iNextSpace : length).Trim());
    }
    
    0 讨论(0)
  • 2020-11-29 23:18

    I took your approach a little further:

    public string TruncateAtWord(string value, int length)
    {
        if (value == null || value.Trim().Length <= length)
            return value;
    
        int index = value.Trim().LastIndexOf(" ");
    
        while ((index + 3) > length)
            index = value.Substring(0, index).Trim().LastIndexOf(" ");
    
        if (index > 0)
            return value.Substring(0, index) + "...";
    
        return value.Substring(0, length - 3) + "...";
    }
    

    I'm using this to truncate tweets.

    0 讨论(0)
  • 2020-11-29 23:18

    Heres what i came up with. This is to get the rest of the sentence also in chunks.

    public static List<string> SplitTheSentenceAtWord(this string originalString, int length)
        {
            try
            {
                List<string> truncatedStrings = new List<string>();
                if (originalString == null || originalString.Trim().Length <= length)
                {
                    truncatedStrings.Add(originalString);
                    return truncatedStrings;
                }
                int index = originalString.Trim().LastIndexOf(" ");
    
                while ((index + 3) > length)
                    index = originalString.Substring(0, index).Trim().LastIndexOf(" ");
    
                if (index > 0)
                {
                    string retValue = originalString.Substring(0, index) + "...";
                    truncatedStrings.Add(retValue);
    
                    string shortWord2 = originalString;
                    if (retValue.EndsWith("..."))
                    {
                        shortWord2 = retValue.Replace("...", "");
                    }
                    shortWord2 = originalString.Substring(shortWord2.Length);
    
                    if (shortWord2.Length > length) //truncate it further
                    {
                        List<string> retValues = SplitTheSentenceAtWord(shortWord2.TrimStart(), length);
                        truncatedStrings.AddRange(retValues);
                    }
                    else
                    {
                        truncatedStrings.Add(shortWord2.TrimStart());
                    }
                    return truncatedStrings;
                }
                var retVal_Last = originalString.Substring(0, length - 3);
                truncatedStrings.Add(retVal_Last + "...");
                if (originalString.Length > length)//truncate it further
                {
                    string shortWord3 = originalString;
                    if (originalString.EndsWith("..."))
                    {
                        shortWord3 = originalString.Replace("...", "");
                    }
                    shortWord3 = originalString.Substring(retVal_Last.Length);
                    List<string> retValues = SplitTheSentenceAtWord(shortWord3.TrimStart(), length);
    
                    truncatedStrings.AddRange(retValues);
                }
                else
                {
                    truncatedStrings.Add(retVal_Last + "...");
                }
                return truncatedStrings;
            }
            catch
            {
                return new List<string> { originalString };
            }
        }
    
    0 讨论(0)
  • 2020-11-29 23:21

    Try the following. It is pretty rudimentary. Just finds the first space starting at the desired length.

    public static string TruncateAtWord(this string value, int length) {
        if (value == null || value.Length < length || value.IndexOf(" ", length) == -1)
            return value;
    
        return value.Substring(0, value.IndexOf(" ", length));
    }
    
    0 讨论(0)
提交回复
热议问题