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 \"
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.
///
/// has no option to ignore casing.
/// This functions mimics but also ignores casing.
/// When called with is used to filter 'empty' entries.
///
/// String to split
/// Array of separators
/// Additional options
///
public static IEnumerable 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;
}