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 \"
Dim arr As String() = Strings.Split("asdf aA asdfget aa uoiu AA",
"aa" ,, CompareMethod.Text)
CompareMethod.Text
ignores case.
In your algorithm, you can use the String.IndexOf method and pass in OrdinalIgnoreCase as the StringComparison parameter.
It's not the pretties version but also works:
"asdf aA asdfget aa uoiu AA".Split(new[] { "aa", "AA", "aA", "Aa" }, StringSplitOptions.RemoveEmptyEntries);
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")
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);
}
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;
}