Trim string from the end of a string in .NET - why is this missing?

前端 未结 13 1331
夕颜
夕颜 2020-12-13 12:08

I need this all the time and am constantly frustrated that the Trim(), TrimStart() and TrimEnd() functions don\'t take strings as inputs. You call EndsWith() on a string, an

相关标签:
13条回答
  • 2020-12-13 12:23

    Using Daniel's code and wrapping it in a while rather than a straight if gives functionality more akin to the Microsoft Trim function:

    public static string TrimEnd(this string input, string suffixToRemove)
    {
        while (input != null && suffixToRemove != null && input.EndsWith(suffixToRemove))
        {
            input = input.Substring(0, input.Length - suffixToRemove.Length);
        }
        return input;
    }
    
    0 讨论(0)
  • 2020-12-13 12:23

    I like my TrimEnd to remove all the instances of the string at the end and not just the last one.

    public static string TrimEnd(this string str, string trimStr)
    {
        if (string.IsNullOrEmpty(str) || string.IsNullOrEmpty(trimStr)) return str;
    
        while(str.EndsWith(trimStr))
        {
            str = str.Remove(str.LastIndexOf(trimStr));
        }
        return str;
    }
    
    0 讨论(0)
  • As far as why the function you want is missing, I suspect it's because the designers didn't see it to be as common or as basic as you think it is. And as you've seen from the other answers, it's an easy enough thing to duplicate.

    Here's the method I use to do it.

    public static string TrimEnd(string input, string suffixToRemove)
    {
        if (input == null)
            throw new ArgumentException("input cannot be null.");
        if (suffixToRemove == null)
            throw new ArgumentException("suffixToRemove cannot be null.");
        int pos = input.LastIndexOf(suffixToRemove);
        if (pos == (input.Length - suffixToRemove.Length))
            return input.Substring(0, pos);
        return input;
    }
    
    0 讨论(0)
  • 2020-12-13 12:29

    Here's the extension method I came up with (heavy inspiration taken from existing answers to this question) to complement the existing TrimEnd method; it takes an optional bool allowing for only removing one trailing instance of the string instead of all trailing instances.

    /// <summary>
    /// Removes trailing occurrence(s) of a given string from the current System.String object.
    /// </summary>
    /// <param name="trimSuffix">A string to remove from the end of the current System.String object.</param>
    /// <param name="removeAll">If true, removes all trailing occurrences of the given suffix; otherwise, just removes the outermost one.</param>
    /// <returns>The string that remains after removal of suffix occurrence(s) of the string in the trimSuffix parameter.</returns>
    public static string TrimEnd(this string input, string trimSuffix, bool removeAll = true) {
        while (input != null && trimSuffix != null && input.EndsWith(trimSuffix)) {
            input = input.Substring(0, input.Length - trimSuffix.Length);
    
            if (!removeAll) {
                return input;
            }
        }
    
        return input;
    }
    
    0 讨论(0)
  • 2020-12-13 12:30

    EDIT - wrapped up into a handy extension method:

    public static string TrimEnd(this string source, string value)
    {
        if (!source.EndsWith(value))
            return source;
    
        return source.Remove(source.LastIndexOf(value));
    }
    

    so you can just do s = s.TrimEnd("DEF");

    0 讨论(0)
  • 2020-12-13 12:33

    This is what you object to having to do?

    if (theString.endsWith(theOtherString))
    {
       theString = theString.Substring(0, theString.Length - theOtherString.Length);
    }
    
    0 讨论(0)
提交回复
热议问题