Is there a way to make the following return true?
string title = "ASTRINGTOTEST";
title.Contains("string");
There doesn't seem to be an overload that allows me to set the case sensitivity..
Currently I UPPERCASE them both, but that's just silly.
UPDATE
The sillyness I refer to is the i18n issues that come with up- and down casing.
Source: Tips4all
You could use IndexOf() and pass StringComparison.OrdinalIgnoreCase
ReplyDeletestring title = "STRING";
bool contains = title.IndexOf("string", StringComparison.OrdinalIgnoreCase) >= 0;
Even better is defining a new extension method for string
public static bool Contains(this string source, string toCheck, StringComparison comp) {
return source.IndexOf(toCheck, comp) >= 0;
}
string title = "STRING";
bool contains = title.Contains("string", StringComparison.OrdinalIgnoreCase);
You can use IndexOf() like this:
ReplyDeletestring title = "STRING";
if (title.IndexOf("string", 0, StringComparison.CurrentCultureIgnoreCase) != -1)
{
// The string exists in the original
}
Since 0 (zero) can be an index, you check against -1.
Alternate solution using Regex:
ReplyDeletebool contains = Regex.Match("StRiNG to search", "string", RegexOptions.IgnoreCase).Success;
One issue with the answer is that it will throw an exception if a string is null. You can add that as a check so it won't:
ReplyDeletepublic static bool Contains(this string source, string toCheck, StringComparison comp)
{
if (string.IsNullOrEmpty(toCheck) || string.IsNullOrEmpty(source))
return true;
return source.IndexOf(toCheck, comp) >= 0;
}
You could always just up or downcase the strings first.
ReplyDeletestring title = "string":
title.ToUpper().Contains("STRING") // returns true
Oops, just saw that last bit. A case insensitive compare would *probably* do the same anyway, and if performance is not an issue, I don't see a problem with creating uppercase copies and comparing those. I could have sworn that I once saw a case-insensitive compare once...
how about this:
ReplyDeletetitle.ToLower().Contains("string".ToLower())
nice and simple...... :)
I know that this is not the C#, but in the framework (VB.NET) there is already such a function
ReplyDeleteDim str As String = "UPPERlower"
Dim b As Boolean = InStr(str, "UpperLower")
C# variant:
string myString = "Hello World";
bool contains = Microsoft.VisualBasic.Strings.InStr(myString, "world");
StringExtension class is the way forward, I've combined a couple of the posts above to give a complete code example:
ReplyDeletepublic static class StringExtensions
{
/// <summary>
/// Allows case insensitive checks
/// </summary>
public static bool Contains(this string source, string toCheck, StringComparison comp)
{
return source.IndexOf(toCheck, comp) >= 0;
}
}
How about this?
ReplyDeletestring.Compare("string", "STRING", new System.Globalization.CultureInfo("en-US"), System.Globalization.CompareOptions.IgnoreCase);
Please try this extender in a static class and use it.
ReplyDeletepublic static bool Contains(this string source, string toCheck, StringComparison comp)
{
return source.IndexOf(toCheck, comp) >= 0;
}