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

前端 未结 13 1332
夕颜
夕颜 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:33

    I recently needed a high performance way to remove single or multiple instances of a string from the start/end of a string. This implementation I came up with is O(n) on the length of the string, avoids expensive allocations, and does not call SubString at all by using a span.

    No Substring hack! (Well, now that I edited my post).

        public static string Trim(this string source, string whatToTrim, int count = -1) 
            => Trim(source, whatToTrim, true, true, count);
    
        public static string TrimStart(this string source, string whatToTrim, int count = -1) 
            => Trim(source, whatToTrim, true, false, count);
    
        public static string TrimEnd(this string source, string whatToTrim, int count = -1) 
            => Trim(source, whatToTrim, false, true, count);
    
        public static string Trim(this string source, string whatToTrim, bool trimStart, bool trimEnd, int numberOfOccurrences)
        {
            // source.IsNotNull(nameof(source));  <-- guard method, define your own
            // whatToTrim.IsNotNull(nameof(whatToTrim));  <-- "
    
            if (numberOfOccurrences == 0 
                || (!trimStart && !trimEnd) 
                || whatToTrim.Length == 0 
                || source.Length < whatToTrim.Length)
                return source;
    
            int start = 0, end = source.Length - 1, trimlen = whatToTrim.Length;
    
            if (trimStart)
                for (int count = 0; start < source.Length; start += trimlen, count++)
                {
                    if (numberOfOccurrences > 0 && count == numberOfOccurrences)
                        break;
                    for (int i = 0; i < trimlen; i++)
                        if ((source[start + i] != whatToTrim[i] && i != trimlen) || source.Length - start < trimlen)
                            goto DONESTART;
                }
    
            DONESTART:
            if (trimEnd)
                for (int count = 0; end > -1; end -= trimlen, count++)
                {
                    if (numberOfOccurrences != -1 && count == numberOfOccurrences)
                        break;
    
                    for (int i = trimlen - 1; i > -1; --i)
                        if ((source[end - trimlen + i + 1] != whatToTrim[i] && i != 0) || end - start + 1 < trimlen)
                            goto DONEEND;
                }
    
            DONEEND:
            return source.AsSpan().Slice(start, end - start + 1).ToString();
        }
    
    0 讨论(0)
  • 2020-12-13 12:34

    The following example demonstrates how to extract individual words from a block of text by treating white space and punctuation marks as delimiters. The character array passed to the separator parameter of the String.Split(Char[]) method consists of a space character and a tab character, together with some common punctuation symbols.

    string words ="sfdgdfg-121";
    string [] split = words.Split(new Char [] {' ', ',', '.', ':', '-' });
    foreach (string s in split)
    {
        if (s.Trim() != "")              
            Console.WriteLine(s);
    }
    

    Try this code.

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

    Trim(), TrimStart() and TrimEnd() are methods which replace all occurrences of the same character. That means you can only remove a series of blanks or a series of dots for example.

    You could use a regular expression replace in order to accomplish this:

    string s1 = "This is a sentence.TRIMTHIS";
    string s2 = System.Text.RegularExpressions.Regex.Replace(s1, @"TRIMTHIS$", "");
    

    You could wrap it in an extension method for convenience:

    public static string TrimStringEnd(this string text, string removeThis)
    {
        return System.Text.RegularExpressions.Regex.Replace(s1, removeThis, "");
    }
    

    And call it this way

    string s2 = (@"This is a sentence.TRIMTHIS").TrimStringEnd(@"TRIMTHIS");
    
    0 讨论(0)
  • 2020-12-13 12:38
    .TrimStart("AB".ToCharArray())
    
    0 讨论(0)
  • 2020-12-13 12:41

    Regex replace may be your friend in this instance.

    var str = "Hello World!";
    str = Regex.Replace(str, @"World!$", "");
    //str == "Hello"
    
    0 讨论(0)
  • 2020-12-13 12:42

    I knocked up this quick extension method.

    Not positive it works (I can't test it right now), but the theory is sound.

        public static string RemoveLast(this string source, string value)
        {
            int index = source.LastIndexOf(value);
            return index != -1 ? source.Remove(index, value.Length) : source;
        }
    
    0 讨论(0)
提交回复
热议问题