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

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 "

Answers


There's no easy way to accomplish this using string.Split. (Well, except for specifying all the permutations of the split string for each char lower/upper case in an array - not very elegant I think you'll agree.)

However, Regex.Split should do the job quite nicely.

Example:

var parts = Regex.Split(input, "aa", RegexOptions.IgnoreCase);

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")

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


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);
}

It's not the pretties version but also works:

"asdf aA asdfget aa uoiu AA".Split(new[] { "aa", "AA", "aA", "Aa" }, StringSplitOptions.RemoveEmptyEntries);

    public static List<string> _Split(this string input,string[] splt)
    {
        List<string> _Result=new List<string>();
        foreach(string _splt in splt)
        {
            if (splt.Count() == 1)
            { 
                _Result.AddRange(Regex.Split(input, _splt, RegexOptions.IgnoreCase).ToList());
            }
            else 
            {
                List<string> NewStr = Regex.Split(input, _splt, RegexOptions.IgnoreCase).ToList();
                foreach(string _NewStr in NewStr)
                {
                    List<string> NewSplt = splt.ToList();
                    NewSplt.Remove(_splt);
                    return _Split(_NewStr, NewSplt.ToArray());
                }
            } 
        }
        return _Result;
    } 

then use this function as bellow

public frmThematicConversation()
{
    InitializeComponent();
    string str = "a b c d e f g h a b c f a d c b f";
    string[] splt = { "a", "b" };
    List<string> _result = str._Split(splt);
}

Dim arr As String() = Strings.Split("asdf aA asdfget aa uoiu AA", 
                                    "aa" ,, CompareMethod.Text)

CompareMethod.Text ignores case.


Need Your Help

Forecasting time series data

r time-series xts

I've done some research and I am stuck in finding the solution. I have a time series data, very basic data frame, let's call it x:

Why does 2>&1 need to come before a | (pipe) but after a "> myfile" (redirect to file)?

bash shell pipe stdout pipeline

When combining stderr with stdout, why does 2&gt;&amp;1 need to come before a | (pipe) but after a &gt; myfile (redirect to file)?