C# TrimStart with string parameter

后端 未结 9 695
轻奢々
轻奢々 2021-02-04 23:00

I\'m looking for String extension methods for TrimStart() and TrimEnd() that accept a string parameter.

I could build one myself but I\'m alw

相关标签:
9条回答
  • 2021-02-04 23:30

    I thought the question was trying to trim a specific string from the start of a larger string.

    For instance, if I had the string "hellohellogoodbyehello", if you tried to call TrimStart("hello") you would get back "goodbyehello".

    If that is the case, you could use code like the following:

    string TrimStart(string source, string toTrim)
    {
        string s = source;
        while (s.StartsWith(toTrim))
        {
            s = s.Substring(toTrim.Length - 1);
        }
        return s;
    }
    

    This wouldn't be super-efficient if you needed to do a lot of string-trimming, but if its just for a few cases, it is simple and gets the job done.

    0 讨论(0)
  • 2021-02-04 23:30

    If you did want one that didn't use the built in trim functions for whatever reasons, assuming you want an input string to use for trimming such as " ~!" to essentially be the same as the built in TrimStart with [' ', '~', '!']

    public static String TrimStart(this string inp, string chars)
    {
        while(chars.Contains(inp[0]))
        {
            inp = inp.Substring(1);
        }
    
        return inp;
    }
    
    public static String TrimEnd(this string inp, string chars)
    {
        while (chars.Contains(inp[inp.Length-1]))
        {
            inp = inp.Substring(0, inp.Length-1);
        }
    
        return inp;
    }
    
    0 讨论(0)
  • 2021-02-04 23:34

    TrimStart and TrimEnd takes in an array of chars. This means that you can pass in a string as a char array like this:

    var trimChars = " .+-";
    var trimmed = myString.TrimStart(trimChars.ToCharArray());
    

    So I don't see the need for an overload that takes a string parameter.

    0 讨论(0)
  • 2021-02-04 23:38

    To trim all occurrences of the (exactly matching) string, you can use something like the following:

    TrimStart

    public static string TrimStart(this string target, string trimString)
    {
        if (string.IsNullOrEmpty(trimString)) return target;
    
        string result = target;
        while (result.StartsWith(trimString))
        {
            result = result.Substring(trimString.Length);
        }
    
        return result;
    }
    

    TrimEnd

    public static string TrimEnd(this string target, string trimString)
    {
        if (string.IsNullOrEmpty(trimString)) return target;
    
        string result = target;
        while (result.EndsWith(trimString))
        {
            result = result.Substring(0, result.Length - trimString.Length);
        }
    
        return result;
    }
    

    To trim any of the characters in trimChars from the start/end of target (e.g. "foobar'@"@';".TrimEnd(";@'") will return "foobar") you can use the following:

    TrimStart

    public static string TrimStart(this string target, string trimChars)
    {
        return target.TrimStart(trimChars.ToCharArray());
    }
    

    TrimEnd

    public static string TrimEnd(this string target, string trimChars)
    {
        return target.TrimEnd(trimChars.ToCharArray());
    }
    
    0 讨论(0)
  • 2021-02-04 23:41

    Function to trim start/end of a string with a string parameter, but only once (no looping, this single case is more popular, loop can be added with an extra param to trigger it) :

    public static class BasicStringExtensions
    {
        public static string TrimStartString(this string str, string trimValue)
        {
            if (str.StartsWith(trimValue))
                return str.TrimStart(trimValue.ToCharArray());
            //otherwise don't modify
            return str;
        }
        public static string TrimEndString(this string str, string trimValue)
        {
            if (str.EndsWith(trimValue))
                return str.TrimEnd(trimValue.ToCharArray());
            //otherwise don't modify
            return str;
        }
    }
    

    As mentioned before, if you want to implement the "while loop" approach, be sure to check for empty string otherwise it can loop forever.

    0 讨论(0)
  • 2021-02-04 23:42

    I'm assuming you mean that, for example, given the string "HelloWorld" and calling the function to 'trim' the start with "Hello" you'd be left with "World". I'd argue that this is really a substring operation as you're removing a portion of the string of known length, rather than a trim operation which removes an unknown length of string.

    As such, we created a couple of extension methods named SubstringAfter and SubstringBefore. It would be nice to have them in the framework, but they aren't so you need to implement them yourselves. Don't forget to have a StringComparison parameter, and to use Ordinal as the default if you make it optional.

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