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

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

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

I hadn't heard of Regex.Split()...thanks! – Michael La Voie Sep 17 '09 at 0:05 No problem. And yeah, it's one of those little hidden gems in the framework.

– Noldorin Sep 17 '09 at 0:06.

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.

So there is no way to take advantage of String. Split? – jdelator Sep 16 '09 at 23:56.

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

Yeah, this is what I meant by permutations - it becomes quite cumbersome for longer split strings. – Noldorin Sep 17 '09 at 0:10.

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. 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.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions