I'm developing a C# 4.5 app and I need a function to return true for the following comparison:
"bla LéOnAr d/o bla".ComplexContains("leonardo")
In other words, I need string.Compare(str1, str2, CultureInfo.InvariantCulture, CompareOptions.IgnoreCase | CompareOptions.IgnoreSymbols | CompareOptions.IgnoreNonSpace)
to also check for "contains!
Can anyone help?
You could use an appropriate CompareInfo
and then CompareInfo.IndexOf(string, string, CompareOptions)
and check the result against -1. Sample:
using System;
using System.Globalization;
class Program
{
static void Main()
{
var compareInfo = CultureInfo.InvariantCulture.CompareInfo;
var options = CompareOptions.IgnoreCase |
CompareOptions.IgnoreSymbols |
CompareOptions.IgnoreNonSpace;
var haystack = "bla Lé OnAr d/o bla";
var needle = "leonardo";
var index = compareInfo.IndexOf(haystack, needle, options);
Console.WriteLine(index); // 4
}
}
Or in method form:
private static bool ComplexContains(string source, string value)
{
var index = CultureInfo.InvariantCulture.CompareInfo.IndexOf
(source, value, CompareOptions.IgnoreCase |
CompareOptions.IgnoreSymbols | CompareOptions.IgnoreNonSpace);
return index != -1;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With