How to split a string while ignoring the case of the delimiter?

前端 未结 8 474
梦谈多话
梦谈多话 2021-01-01 09:56

I need to split a string let\'s say \"asdf aA asdfget aa uoiu AA\" split using \"aa\" ignoring the case. to

\"asdf \"
\"asdfget \"
\"uoiu \"
相关标签:
8条回答
  • 2021-01-01 10:06
    Dim arr As String() = Strings.Split("asdf aA asdfget aa uoiu AA", 
                                        "aa" ,, CompareMethod.Text)
    

    CompareMethod.Text ignores case.

    0 讨论(0)
  • 2021-01-01 10:10

    In your algorithm, you can use the String.IndexOf method and pass in OrdinalIgnoreCase as the StringComparison parameter.

    0 讨论(0)
  • 2021-01-01 10:12

    It's not the pretties version but also works:

    "asdf aA asdfget aa uoiu AA".Split(new[] { "aa", "AA", "aA", "Aa" }, StringSplitOptions.RemoveEmptyEntries);
    
    0 讨论(0)
  • 2021-01-01 10:16

    If you don't care about case, then the simplest thing to do is force the string to all uppercase or lowercase before using split.

    stringbits = datastring.ToLower().Split("aa")
    

    If you care about case for the interesting bits of the string but not the separators then I would use String.Replace to force all the separators to a specific case (upper or lower, doesn't matter) and then call String.Split using the matching case for the separator.

    strinbits = datastring.Replace("aA", "aa").Replace("AA", "aa").Split("aa")
    
    0 讨论(0)
  • 2021-01-01 10:19

    My answer isn't as good as Noldorin's, but I'll leave it so people can see the alternative method. This isn't as good for simple splits, but it is more flexible if you need to do more complex parsing.

    using System.Text.RegularExpressions;
    
    string data = "asdf aA asdfget aa uoiu AA";
    string aaRegex = "(.+?)[aA]{2}";
    
    MatchCollection mc = Regex.Matches(data, aaRegex);
    
    foreach(Match m in mc)
    {
        Console.WriteLine(m.Value);
    }
    
    0 讨论(0)
  • 2021-01-01 10:19

    Building on the answer from @Noldorin i made this extension method.

    It takes in more than one seperator string, and mimics the behavior of string.Split(..) if you supply several seperator strings. It has invariant ('culture-unspecific') culture and ignores cases of course.

    /// <summary>
    /// <see cref="string.Split(char[])"/> has no option to ignore casing.
    /// This functions mimics <see cref="string.Split(char[])"/> but also ignores casing.
    /// When called with <see cref="StringSplitOptions.RemoveEmptyEntries"/> <see cref="string.IsNullOrWhiteSpace(string)"/> is used to filter 'empty' entries.
    /// </summary>
    /// <param name="input">String to split</param>
    /// <param name="separators">Array of separators</param>
    /// <param name="options">Additional options</param>
    /// <returns></returns>
    public static IEnumerable<string> SplitInvariantIgnoreCase(this string input, string[] separators, StringSplitOptions options)
    {
        if (separators == null) throw new ArgumentNullException(nameof(separators));
        if (separators.Length <= 0) throw new ArgumentException("Value cannot be an empty collection.", nameof(separators));
        if (string.IsNullOrWhiteSpace(input)) throw new ArgumentException("Value cannot be null or whitespace.", nameof(input));
    
        // Build a regex pattern of all the separators this looks like aa|bb|cc
        // The Pipe character '|' means alternative.
        var regexPattern = string.Join("|", separators);
    
        var regexSplitResult = Regex.Split(input, regexPattern, RegexOptions.IgnoreCase | RegexOptions.CultureInvariant);
    
        // NOTE To be honest - i don't know the exact behaviour of Regex.Split when it comes to empty entries.
        //      Therefore i doubt that filtering null values even matters - however for consistency i decided to code it in anyways.
        return options.HasFlag(StringSplitOptions.RemoveEmptyEntries) ? 
            regexSplitResult.Where(c => !string.IsNullOrWhiteSpace(c)) 
            : regexSplitResult;
    }
    
    0 讨论(0)
提交回复
热议问题