ignore case

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

is there a way to look at a string and ignore the case . i do not want to
modify the string though.

ie. i have a string and want to search for a substring "XYZ" but "XYZ" can
be upper/lower/combination
 
string s = "aBcDXyZeFgHi";
Console.WriteLine (s.IndexOf("xYz", CompareOptions.IgnoreCase);

Thanks,
Michael C., MCDBA
 
Hi Sarah,

You can put both strings in the same capitalization before test:

if(name.ToUpper() == "sarah".ToUpper())
{
. . .
}

and you can do it with regular expressions too.

Regards,
Paulo Gomes
 
Michael C said:
string s = "aBcDXyZeFgHi";
Console.WriteLine (s.IndexOf("xYz", CompareOptions.IgnoreCase);

While using CompareOptions.IgnoreCase is indeed a great way to go (it
saves you having to create two new strings) the above doesn't quite
work. Instead, you need to use something like:

CompareInfo ci = CultureInfo.CurrentCulture.CompareInfo;
Console.WriteLine (ci.IndexOf(s, "xYz", CompareOptions.IgnoreCase));
 
Good catch. Sorry for the typo - I really need to quit typing when I'm
tired :)

By the way, for the OP - you need to use the System.Globalization namespace
to use CompareInfo.

Thanks,
Michael C., MCDBA
 
Back
Top