Case Insensitive comparison in C# [duplicate] - c#

This question already has answers here:
Case insensitive 'Contains(string)'
(29 answers)
Closed 9 years ago.
I am comparing two strings using following code
string1.Contains(string2)
but i am not getting results for case insensitive search. Moreover I cant use String.Compare coz i dont want to match the whole name as the name is very big.
My need is to have case insensitive search and the search text can be of any length which the String1 contains.
Eg Term************** is the name.
I enter "erm" in textbox den i get the result. but when i enter "term" i dont get any result.
Can anyone help me :)

Try this:
string.Equals("this will return true", "ThIs WiLL ReTurN TRue", StringComparison.CurrentCultureIgnoreCase)
Or, for contains:
if (string1.IndexOf(string2, StringComparison.CurrentCultureIgnoreCase) >= 0)

I prefer an extension method like this.
public static class StringExtensions
{
public static bool Contains(this string source, string value, StringComparison compareMode)
{
if (string.IsNullOrEmpty(source))
return false;
return source.IndexOf(value, compareMode) >= 0;
}
}
Notice that in this way you could avoid the costly transformation in upper or lower case.
You could call the extension using this syntax
bool result = "This is a try".Contains("TRY", StringComparison.InvariantCultureIgnoreCase);
Console.WriteLine(result);
Please note: the above extension (as true for every extension method) should be defined inside a non-nested, non-generic static class See MSDN Ref

Convert both strings to a same case, either upper or lower.
string1.ToUpper().Contains(string2.ToUpper());

Why not this:
if (string1.IndexOf(string2, StringComparison.OrdinalIgnoreCase) >= 0)
{
}

string1.ToUpperInvariant().Contains(string2.ToUpperInvariant());

You can either convert both strings to uppercase, or use regular expressions:
using System.Text.RegularExpressions;
class Program {
static void Main(string[] args) {
string string1 = "TermSomething";
string string2 = "term";
bool test1 = string1.ToUpperInvariant().Contains(string2.ToUpperInvariant());
bool test2 = Regex.IsMatch(string1, Regex.Escape(string2), RegexOptions.IgnoreCase);
}
}
Note that if you use regular expressions you should escape the search string, so that special regex characters are interpreted literally.

Regex.IsMatch(string1,string2,RegexOptions.IgnoreCase);
This returns boolean value.....

Related

Is there any waay to ignore cases in c# using .Contains()? [duplicate]

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 (by which I am referring to the i18n issues that come with up- and down casing).
UPDATE
This question is ancient and since then I have realized I asked for a simple answer for a really vast and difficult topic if you care to investigate it fully.
For most cases, in mono-lingual, English code bases this answer will suffice. I'm suspecting because most people coming here fall in this category this is the most popular answer.
This answer however brings up the inherent problem that we can't compare text case insensitive until we know both texts are the same culture and we know what that culture is. This is maybe a less popular answer, but I think it is more correct and that's why I marked it as such.
You could use the String.IndexOf Method and pass StringComparison.OrdinalIgnoreCase as the type of search to use:
string title = "STRING";
bool contains = title.IndexOf("string", StringComparison.OrdinalIgnoreCase) >= 0;
Even better is defining a new extension method for string:
public static class StringExtensions
{
public static bool Contains(this string source, string toCheck, StringComparison comp)
{
return source?.IndexOf(toCheck, comp) >= 0;
}
}
Note, that null propagation ?. is available since C# 6.0 (VS 2015), for older versions use
if (source == null) return false;
return source.IndexOf(toCheck, comp) >= 0;
USAGE:
string title = "STRING";
bool contains = title.Contains("string", StringComparison.OrdinalIgnoreCase);
To test if the string paragraph contains the string word (thanks #QuarterMeister)
culture.CompareInfo.IndexOf(paragraph, word, CompareOptions.IgnoreCase) >= 0
Where culture is the instance of CultureInfo describing the language that the text is written in.
This solution is transparent about the definition of case-insensitivity, which is language dependent. For example, the English language uses the characters I and i for the upper and lower case versions of the ninth letter, whereas the Turkish language uses these characters for the eleventh and twelfth letters of its 29 letter-long alphabet. The Turkish upper case version of 'i' is the unfamiliar character 'İ'.
Thus the strings tin and TIN are the same word in English, but different words in Turkish. As I understand, one means 'spirit' and the other is an onomatopoeia word. (Turks, please correct me if I'm wrong, or suggest a better example)
To summarise, you can only answer the question 'are these two strings the same but in different cases' if you know what language the text is in. If you don't know, you'll have to take a punt. Given English's hegemony in software, you should probably resort to CultureInfo.InvariantCulture, because it will be wrong in familiar ways.
You can use IndexOf() like this:
string title = "STRING";
if (title.IndexOf("string", 0, StringComparison.OrdinalIgnoreCase) != -1)
{
// The string exists in the original
}
Since 0 (zero) can be an index, you check against -1.
Microsoft .NET Documentation:
The zero-based index position of the value parameter from the start of the current instance if that string is found, or -1 if it is not. If value is Empty, the return value is startIndex.
.NET Core 2.0+ (including .NET 5.0+)
.NET Core has had a pair of methods to deal with this since version 2.0 :
String.Contains(Char, StringComparison)
String.Contains(String, StringComparison)
Example:
"Test".Contains("test", System.StringComparison.CurrentCultureIgnoreCase);
It is now officially part of the .NET Standard 2.1, and therefore part of all the implementations of the Base Class Library that implement this version of the standard (or a higher one).
Alternative solution using Regex:
bool contains = Regex.IsMatch("StRiNG to search", Regex.Escape("string"), RegexOptions.IgnoreCase);
You could always just up or downcase the strings first.
string 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...
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:
public 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;
}
StringExtension class is the way forward, I've combined a couple of the posts above to give a complete code example:
public 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;
}
}
OrdinalIgnoreCase, CurrentCultureIgnoreCase or InvariantCultureIgnoreCase?
Since this is missing, here are some recommendations about when to use which one:
Dos
Use StringComparison.OrdinalIgnoreCase for comparisons
as your safe default for culture-agnostic string matching.
Use StringComparison.OrdinalIgnoreCase comparisons
for increased speed.
Use StringComparison.CurrentCulture-based string operations
when displaying the output to the user.
Switch current use of string operations based on the invariant
culture to use the non-linguistic StringComparison.Ordinal or StringComparison.OrdinalIgnoreCase when the comparison is
linguistically irrelevant (symbolic, for example).
Use ToUpperInvariant rather than ToLowerInvariant when
normalizing strings for comparison.
Don'ts
Use overloads for string operations that don't explicitly
or implicitly specify the string comparison mechanism.
Use StringComparison.InvariantCulture -based string
operations in most cases; one of the few exceptions would be
persisting linguistically meaningful but culturally-agnostic data.
Based on these rules you should use:
string title = "STRING";
if (title.IndexOf("string", 0, StringComparison.[YourDecision]) != -1)
{
// The string exists in the original
}
whereas [YourDecision] depends on the recommendations from above.
link of source: http://msdn.microsoft.com/en-us/library/ms973919.aspx
This is clean and simple.
Regex.IsMatch(file, fileNamestr, RegexOptions.IgnoreCase)
These are the easiest solutions.
By Index of
string title = "STRING";
if (title.IndexOf("string", 0, StringComparison.CurrentCultureIgnoreCase) != -1)
{
// contains
}
By Changing case
string title = "STRING";
bool contains = title.ToLower().Contains("string")
By Regex
Regex.IsMatch(title, "string", RegexOptions.IgnoreCase);
As simple and works
title.ToLower().Contains("String".ToLower())
Just like this:
string s="AbcdEf";
if(s.ToLower().Contains("def"))
{
Console.WriteLine("yes");
}
I know that this is not the C#, but in the framework (VB.NET) there is already such a function
Dim 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");
You can use a string comparison parameter (available from .NET Core 2.1 and above) String.Contains Method.
public bool Contains (string value, StringComparison comparisonType);
Example:
string title = "ASTRINGTOTEST";
title.Contains("string", StringComparison.InvariantCultureIgnoreCase);
The InStr method from the VisualBasic assembly is the best if you have a concern about internationalization (or you could reimplement it). Looking at in it dotNeetPeek shows that not only does it account for caps and lowercase, but also for kana type and full- vs. half-width characters (mostly relevant for Asian languages, although there are full-width versions of the Roman alphabet too). I'm skipping over some details, but check out the private method InternalInStrText:
private static int InternalInStrText(int lStartPos, string sSrc, string sFind)
{
int num = sSrc == null ? 0 : sSrc.Length;
if (lStartPos > num || num == 0)
return -1;
if (sFind == null || sFind.Length == 0)
return lStartPos;
else
return Utils.GetCultureInfo().CompareInfo.IndexOf(sSrc, sFind, lStartPos, CompareOptions.IgnoreCase | CompareOptions.IgnoreKanaType | CompareOptions.IgnoreWidth);
}
Use this:
string.Compare("string", "STRING", new System.Globalization.CultureInfo("en-US"), System.Globalization.CompareOptions.IgnoreCase);
This is quite similar to other example here, but I've decided to simplify enum to bool, primary because other alternatives are normally not needed. Here is my example:
public static class StringExtensions
{
public static bool Contains(this string source, string toCheck, bool bCaseInsensitive )
{
return source.IndexOf(toCheck, bCaseInsensitive ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal) >= 0;
}
}
And usage is something like:
if( "main String substring".Contains("SUBSTRING", true) )
....
Using a RegEx is a straight way to do this:
Regex.IsMatch(title, "string", RegexOptions.IgnoreCase);
Just to build on the answer here, you can create a string extension method to make this a little more user-friendly:
public static bool ContainsIgnoreCase(this string paragraph, string word)
{
return CultureInfo.CurrentCulture.CompareInfo.IndexOf(paragraph, word, CompareOptions.IgnoreCase) >= 0;
}
if you want to check if your passed string is in string then there is a simple method for that.
string yourStringForCheck= "abc";
string stringInWhichWeCheck= "Test abc abc";
bool isContained = stringInWhichWeCheck.ToLower().IndexOf(yourStringForCheck.ToLower()) > -1;
This boolean value will return if the string is contained or not
Similar to previous answers (using an extension method) but with two simple null checks (C# 6.0 and above):
public static bool ContainsIgnoreCase(this string source, string substring)
{
return source?.IndexOf(substring ?? "", StringComparison.OrdinalIgnoreCase) >= 0;
}
If source is null, return false (via null-propagation operator ?.)
If substring is null, treat as an empty string and return true (via null-coalescing operator ??)
The StringComparison can of course be sent as a parameter if needed.
The top-rated several answers are all good and correct in their own ways, I write here to add more information, context, and perspective.
For clarity, let us consider that string A contains string B if there is any subsequence of codepoints in A which is equal to B. If we accept this, the problem is reduced to the question of whether two strings are equal.
The question of when strings are equal has been considered in detail for many decades. Much of the present state of knowledge is encapsulated in SQL collations. Unicode normal forms are close to a proper subset of this. But there is more beyond even SQL collations.
For example, in SQL collations, you can be
Strictly binary sensitive - so that different Unicode normalisation forms (e.g. precombined or combining accents) compare differently.
For example, é can be represented as either U+00e9 (precombined) or U+0065 U+0301 (e with combining acute accent).
Are these the same or different?
Unicode normalised - In this case the above examples would be equal to each other, but not to É or e.
accent insensitive, (for e.g. Spanish, German, Swedish etc. text). In this case U+0065 = U+0065 U+0301 = U+00e9 = é = e
case and accent insensitive, so that (for e.g. Spanish, German, Swedish etc. text). In this case U+00e9 = U+0065 U+0301 = U+00c9 = U+0045 U+0301 = U+0049 = U+0065 = E = e = É = é
Kanatype sensitive or insensitive, i.e. you can consider Japanese Hiragana and Katakana as equivalent or different. The two syllabaries contain the same number of characters, organised and pronounced in the (mostly) the same way, but written differently and used for different purposes. For example katakana are used for loan words or foreign names, but hiragana are used for children's books, pronunciation guides (e.g. rubies), and where there is no kanji for a word (or perhaps where the writer does not know the kanji, or thinks the reader may not know it).
Full-width or half-width sensitive - Japanese encodings include two representations of some characters for historical reasons - they were displayed at different sizes.
Ligatures considered equivalent or not: See https://en.wikipedia.org/wiki/Ligature_(writing)
Is æ the same as ae or not? They have different Unicode encodings, as do accented characters, but unlike accented characters they also look different.
Which brings us to...
Arabic presentation form equivalence
Arabic writing has a culture of beautiful calligraphy, where particular sequences of adjacent letters have specific representations. Many of these have been encoded in the Unicode standard. I don't fully understand the rules, but they seem to me to be analogous to ligatures.
Other scripts and systems: I have no knowledge whatsoever or Kannada, Malayalam, Sinhala, Thai, Gujarati, Tibetan, or almost all of the tens or hundreds of scripts not mentioned. I assume they have similar issues for the programmer, and given the number of issues mentioned so far and for so few scripts, they probably also have additional issues the programmer ought to consider.
That gets us out of the "encoding" weeds.
Now we must enter the "meaning" weeds.
is Beijing equal to 北京? If not, is Bĕijīng equal to 北京? If not, why not? It is the Pinyin romanisation.
Is Peking equal to 北京? If not, why not? It is the Wade-Giles romanisation.
Is Beijing equal to Peking? If not, why not?
Why are you doing this anyway?
For example, if you want to know if it is possible that two strings (A and B) refer to the same geographical location, or same person, you might want to ask:
Could these strings be either Wade-Giles or Pinyin representations of a set of sequences of Chinese characters? If so, is there any overlap between the corresponding sets?
Could one of these strings be a Cyrillic transcription of a Chinese Character?
could one of these strings be a Cyrillic transliteration of the Pinyin romanisation?
Could one of these strings be a Cyrillic transliteration of a Pinyin romanisation of a Sinification of an English name?
Clearly these are difficult questions, which don't have firm answers, and in any case, the answer may be different according to the purpose of the question.
To finish with a concrete example.
If you are delivering a letter or parcel, clearly Beijing, Peking, Bĕijīng and 北京 are all equal. For that purpose, they are all equally good. No doubt the Chinese post-offices recognise many other options, such as Pékin in French, Pequim in Portuguese, Bắc Kinh in Vietnamese, and Бээжин in Mongolian.
Words do not have fixed meanings.
Words are tools we use to navigate the world, to accomplish our tasks, and to communicate with other people.
While it looks like it would be helpful if words like equality, Beijing, or meaning had fixed meanings, the sad fact is they do not.
Yet we seem to muddle along somehow.
TL;DR: If you are dealing with questions relating to reality, in all its nebulosity (cloudiness, uncertainty, lack of clear boundaries), there are basically three possible answers to every question:
Probably
Probably not
Maybe
if ("strcmpstring1".IndexOf(Convert.ToString("strcmpstring2"), StringComparison.CurrentCultureIgnoreCase) >= 0){return true;}else{return false;}
You can use string.indexof () function. This will be case insensitive
The trick here is to look for the string, ignoring case, but to keep it exactly the same (with the same case).
var s="Factory Reset";
var txt="reset";
int first = s.IndexOf(txt, StringComparison.InvariantCultureIgnoreCase) + txt.Length;
var subString = s.Substring(first - txt.Length, txt.Length);
Output is "Reset"
public static class StringExtension
{
#region Public Methods
public static bool ExContains(this string fullText, string value)
{
return ExIndexOf(fullText, value) > -1;
}
public static bool ExEquals(this string text, string textToCompare)
{
return text.Equals(textToCompare, StringComparison.OrdinalIgnoreCase);
}
public static bool ExHasAllEquals(this string text, params string[] textArgs)
{
for (int index = 0; index < textArgs.Length; index++)
if (ExEquals(text, textArgs[index]) == false) return false;
return true;
}
public static bool ExHasEquals(this string text, params string[] textArgs)
{
for (int index = 0; index < textArgs.Length; index++)
if (ExEquals(text, textArgs[index])) return true;
return false;
}
public static bool ExHasNoEquals(this string text, params string[] textArgs)
{
return ExHasEquals(text, textArgs) == false;
}
public static bool ExHasNotAllEquals(this string text, params string[] textArgs)
{
for (int index = 0; index < textArgs.Length; index++)
if (ExEquals(text, textArgs[index])) return false;
return true;
}
/// <summary>
/// Reports the zero-based index of the first occurrence of the specified string
/// in the current System.String object using StringComparison.InvariantCultureIgnoreCase.
/// A parameter specifies the type of search to use for the specified string.
/// </summary>
/// <param name="fullText">
/// The string to search inside.
/// </param>
/// <param name="value">
/// The string to seek.
/// </param>
/// <returns>
/// The index position of the value parameter if that string is found, or -1 if it
/// is not. If value is System.String.Empty, the return value is 0.
/// </returns>
/// <exception cref="ArgumentNullException">
/// fullText or value is null.
/// </exception>
public static int ExIndexOf(this string fullText, string value)
{
return fullText.IndexOf(value, StringComparison.OrdinalIgnoreCase);
}
public static bool ExNotEquals(this string text, string textToCompare)
{
return ExEquals(text, textToCompare) == false;
}
#endregion Public Methods
}
Based on the existing answers and on the documentation of Contains method I would recommend the creation of the following extension which also takes care of the corner cases:
public static class VStringExtensions
{
public static bool Contains(this string source, string toCheck, StringComparison comp)
{
if (toCheck == null)
{
throw new ArgumentNullException(nameof(toCheck));
}
if (source.Equals(string.Empty))
{
return false;
}
if (toCheck.Equals(string.Empty))
{
return true;
}
return source.IndexOf(toCheck, comp) >= 0;
}
}
Simple way for newbie:
title.ToLower().Contains("string");//of course "string" is lowercase.

Ignore Case Sensitive in Linq [duplicate]

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 (by which I am referring to the i18n issues that come with up- and down casing).
UPDATE
This question is ancient and since then I have realized I asked for a simple answer for a really vast and difficult topic if you care to investigate it fully.
For most cases, in mono-lingual, English code bases this answer will suffice. I'm suspecting because most people coming here fall in this category this is the most popular answer.
This answer however brings up the inherent problem that we can't compare text case insensitive until we know both texts are the same culture and we know what that culture is. This is maybe a less popular answer, but I think it is more correct and that's why I marked it as such.
You could use the String.IndexOf Method and pass StringComparison.OrdinalIgnoreCase as the type of search to use:
string title = "STRING";
bool contains = title.IndexOf("string", StringComparison.OrdinalIgnoreCase) >= 0;
Even better is defining a new extension method for string:
public static class StringExtensions
{
public static bool Contains(this string source, string toCheck, StringComparison comp)
{
return source?.IndexOf(toCheck, comp) >= 0;
}
}
Note, that null propagation ?. is available since C# 6.0 (VS 2015), for older versions use
if (source == null) return false;
return source.IndexOf(toCheck, comp) >= 0;
USAGE:
string title = "STRING";
bool contains = title.Contains("string", StringComparison.OrdinalIgnoreCase);
To test if the string paragraph contains the string word (thanks #QuarterMeister)
culture.CompareInfo.IndexOf(paragraph, word, CompareOptions.IgnoreCase) >= 0
Where culture is the instance of CultureInfo describing the language that the text is written in.
This solution is transparent about the definition of case-insensitivity, which is language dependent. For example, the English language uses the characters I and i for the upper and lower case versions of the ninth letter, whereas the Turkish language uses these characters for the eleventh and twelfth letters of its 29 letter-long alphabet. The Turkish upper case version of 'i' is the unfamiliar character 'İ'.
Thus the strings tin and TIN are the same word in English, but different words in Turkish. As I understand, one means 'spirit' and the other is an onomatopoeia word. (Turks, please correct me if I'm wrong, or suggest a better example)
To summarise, you can only answer the question 'are these two strings the same but in different cases' if you know what language the text is in. If you don't know, you'll have to take a punt. Given English's hegemony in software, you should probably resort to CultureInfo.InvariantCulture, because it will be wrong in familiar ways.
You can use IndexOf() like this:
string title = "STRING";
if (title.IndexOf("string", 0, StringComparison.OrdinalIgnoreCase) != -1)
{
// The string exists in the original
}
Since 0 (zero) can be an index, you check against -1.
Microsoft .NET Documentation:
The zero-based index position of the value parameter from the start of the current instance if that string is found, or -1 if it is not. If value is Empty, the return value is startIndex.
.NET Core 2.0+ (including .NET 5.0+)
.NET Core has had a pair of methods to deal with this since version 2.0 :
String.Contains(Char, StringComparison)
String.Contains(String, StringComparison)
Example:
"Test".Contains("test", System.StringComparison.CurrentCultureIgnoreCase);
It is now officially part of the .NET Standard 2.1, and therefore part of all the implementations of the Base Class Library that implement this version of the standard (or a higher one).
Alternative solution using Regex:
bool contains = Regex.IsMatch("StRiNG to search", Regex.Escape("string"), RegexOptions.IgnoreCase);
You could always just up or downcase the strings first.
string 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...
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:
public 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;
}
StringExtension class is the way forward, I've combined a couple of the posts above to give a complete code example:
public 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;
}
}
OrdinalIgnoreCase, CurrentCultureIgnoreCase or InvariantCultureIgnoreCase?
Since this is missing, here are some recommendations about when to use which one:
Dos
Use StringComparison.OrdinalIgnoreCase for comparisons
as your safe default for culture-agnostic string matching.
Use StringComparison.OrdinalIgnoreCase comparisons
for increased speed.
Use StringComparison.CurrentCulture-based string operations
when displaying the output to the user.
Switch current use of string operations based on the invariant
culture to use the non-linguistic StringComparison.Ordinal or StringComparison.OrdinalIgnoreCase when the comparison is
linguistically irrelevant (symbolic, for example).
Use ToUpperInvariant rather than ToLowerInvariant when
normalizing strings for comparison.
Don'ts
Use overloads for string operations that don't explicitly
or implicitly specify the string comparison mechanism.
Use StringComparison.InvariantCulture -based string
operations in most cases; one of the few exceptions would be
persisting linguistically meaningful but culturally-agnostic data.
Based on these rules you should use:
string title = "STRING";
if (title.IndexOf("string", 0, StringComparison.[YourDecision]) != -1)
{
// The string exists in the original
}
whereas [YourDecision] depends on the recommendations from above.
link of source: http://msdn.microsoft.com/en-us/library/ms973919.aspx
This is clean and simple.
Regex.IsMatch(file, fileNamestr, RegexOptions.IgnoreCase)
These are the easiest solutions.
By Index of
string title = "STRING";
if (title.IndexOf("string", 0, StringComparison.CurrentCultureIgnoreCase) != -1)
{
// contains
}
By Changing case
string title = "STRING";
bool contains = title.ToLower().Contains("string")
By Regex
Regex.IsMatch(title, "string", RegexOptions.IgnoreCase);
As simple and works
title.ToLower().Contains("String".ToLower())
Just like this:
string s="AbcdEf";
if(s.ToLower().Contains("def"))
{
Console.WriteLine("yes");
}
I know that this is not the C#, but in the framework (VB.NET) there is already such a function
Dim 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");
You can use a string comparison parameter (available from .NET Core 2.1 and above) String.Contains Method.
public bool Contains (string value, StringComparison comparisonType);
Example:
string title = "ASTRINGTOTEST";
title.Contains("string", StringComparison.InvariantCultureIgnoreCase);
The InStr method from the VisualBasic assembly is the best if you have a concern about internationalization (or you could reimplement it). Looking at in it dotNeetPeek shows that not only does it account for caps and lowercase, but also for kana type and full- vs. half-width characters (mostly relevant for Asian languages, although there are full-width versions of the Roman alphabet too). I'm skipping over some details, but check out the private method InternalInStrText:
private static int InternalInStrText(int lStartPos, string sSrc, string sFind)
{
int num = sSrc == null ? 0 : sSrc.Length;
if (lStartPos > num || num == 0)
return -1;
if (sFind == null || sFind.Length == 0)
return lStartPos;
else
return Utils.GetCultureInfo().CompareInfo.IndexOf(sSrc, sFind, lStartPos, CompareOptions.IgnoreCase | CompareOptions.IgnoreKanaType | CompareOptions.IgnoreWidth);
}
Use this:
string.Compare("string", "STRING", new System.Globalization.CultureInfo("en-US"), System.Globalization.CompareOptions.IgnoreCase);
This is quite similar to other example here, but I've decided to simplify enum to bool, primary because other alternatives are normally not needed. Here is my example:
public static class StringExtensions
{
public static bool Contains(this string source, string toCheck, bool bCaseInsensitive )
{
return source.IndexOf(toCheck, bCaseInsensitive ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal) >= 0;
}
}
And usage is something like:
if( "main String substring".Contains("SUBSTRING", true) )
....
Using a RegEx is a straight way to do this:
Regex.IsMatch(title, "string", RegexOptions.IgnoreCase);
Just to build on the answer here, you can create a string extension method to make this a little more user-friendly:
public static bool ContainsIgnoreCase(this string paragraph, string word)
{
return CultureInfo.CurrentCulture.CompareInfo.IndexOf(paragraph, word, CompareOptions.IgnoreCase) >= 0;
}
if you want to check if your passed string is in string then there is a simple method for that.
string yourStringForCheck= "abc";
string stringInWhichWeCheck= "Test abc abc";
bool isContained = stringInWhichWeCheck.ToLower().IndexOf(yourStringForCheck.ToLower()) > -1;
This boolean value will return if the string is contained or not
Similar to previous answers (using an extension method) but with two simple null checks (C# 6.0 and above):
public static bool ContainsIgnoreCase(this string source, string substring)
{
return source?.IndexOf(substring ?? "", StringComparison.OrdinalIgnoreCase) >= 0;
}
If source is null, return false (via null-propagation operator ?.)
If substring is null, treat as an empty string and return true (via null-coalescing operator ??)
The StringComparison can of course be sent as a parameter if needed.
The top-rated several answers are all good and correct in their own ways, I write here to add more information, context, and perspective.
For clarity, let us consider that string A contains string B if there is any subsequence of codepoints in A which is equal to B. If we accept this, the problem is reduced to the question of whether two strings are equal.
The question of when strings are equal has been considered in detail for many decades. Much of the present state of knowledge is encapsulated in SQL collations. Unicode normal forms are close to a proper subset of this. But there is more beyond even SQL collations.
For example, in SQL collations, you can be
Strictly binary sensitive - so that different Unicode normalisation forms (e.g. precombined or combining accents) compare differently.
For example, é can be represented as either U+00e9 (precombined) or U+0065 U+0301 (e with combining acute accent).
Are these the same or different?
Unicode normalised - In this case the above examples would be equal to each other, but not to É or e.
accent insensitive, (for e.g. Spanish, German, Swedish etc. text). In this case U+0065 = U+0065 U+0301 = U+00e9 = é = e
case and accent insensitive, so that (for e.g. Spanish, German, Swedish etc. text). In this case U+00e9 = U+0065 U+0301 = U+00c9 = U+0045 U+0301 = U+0049 = U+0065 = E = e = É = é
Kanatype sensitive or insensitive, i.e. you can consider Japanese Hiragana and Katakana as equivalent or different. The two syllabaries contain the same number of characters, organised and pronounced in the (mostly) the same way, but written differently and used for different purposes. For example katakana are used for loan words or foreign names, but hiragana are used for children's books, pronunciation guides (e.g. rubies), and where there is no kanji for a word (or perhaps where the writer does not know the kanji, or thinks the reader may not know it).
Full-width or half-width sensitive - Japanese encodings include two representations of some characters for historical reasons - they were displayed at different sizes.
Ligatures considered equivalent or not: See https://en.wikipedia.org/wiki/Ligature_(writing)
Is æ the same as ae or not? They have different Unicode encodings, as do accented characters, but unlike accented characters they also look different.
Which brings us to...
Arabic presentation form equivalence
Arabic writing has a culture of beautiful calligraphy, where particular sequences of adjacent letters have specific representations. Many of these have been encoded in the Unicode standard. I don't fully understand the rules, but they seem to me to be analogous to ligatures.
Other scripts and systems: I have no knowledge whatsoever or Kannada, Malayalam, Sinhala, Thai, Gujarati, Tibetan, or almost all of the tens or hundreds of scripts not mentioned. I assume they have similar issues for the programmer, and given the number of issues mentioned so far and for so few scripts, they probably also have additional issues the programmer ought to consider.
That gets us out of the "encoding" weeds.
Now we must enter the "meaning" weeds.
is Beijing equal to 北京? If not, is Bĕijīng equal to 北京? If not, why not? It is the Pinyin romanisation.
Is Peking equal to 北京? If not, why not? It is the Wade-Giles romanisation.
Is Beijing equal to Peking? If not, why not?
Why are you doing this anyway?
For example, if you want to know if it is possible that two strings (A and B) refer to the same geographical location, or same person, you might want to ask:
Could these strings be either Wade-Giles or Pinyin representations of a set of sequences of Chinese characters? If so, is there any overlap between the corresponding sets?
Could one of these strings be a Cyrillic transcription of a Chinese Character?
could one of these strings be a Cyrillic transliteration of the Pinyin romanisation?
Could one of these strings be a Cyrillic transliteration of a Pinyin romanisation of a Sinification of an English name?
Clearly these are difficult questions, which don't have firm answers, and in any case, the answer may be different according to the purpose of the question.
To finish with a concrete example.
If you are delivering a letter or parcel, clearly Beijing, Peking, Bĕijīng and 北京 are all equal. For that purpose, they are all equally good. No doubt the Chinese post-offices recognise many other options, such as Pékin in French, Pequim in Portuguese, Bắc Kinh in Vietnamese, and Бээжин in Mongolian.
Words do not have fixed meanings.
Words are tools we use to navigate the world, to accomplish our tasks, and to communicate with other people.
While it looks like it would be helpful if words like equality, Beijing, or meaning had fixed meanings, the sad fact is they do not.
Yet we seem to muddle along somehow.
TL;DR: If you are dealing with questions relating to reality, in all its nebulosity (cloudiness, uncertainty, lack of clear boundaries), there are basically three possible answers to every question:
Probably
Probably not
Maybe
if ("strcmpstring1".IndexOf(Convert.ToString("strcmpstring2"), StringComparison.CurrentCultureIgnoreCase) >= 0){return true;}else{return false;}
You can use string.indexof () function. This will be case insensitive
The trick here is to look for the string, ignoring case, but to keep it exactly the same (with the same case).
var s="Factory Reset";
var txt="reset";
int first = s.IndexOf(txt, StringComparison.InvariantCultureIgnoreCase) + txt.Length;
var subString = s.Substring(first - txt.Length, txt.Length);
Output is "Reset"
public static class StringExtension
{
#region Public Methods
public static bool ExContains(this string fullText, string value)
{
return ExIndexOf(fullText, value) > -1;
}
public static bool ExEquals(this string text, string textToCompare)
{
return text.Equals(textToCompare, StringComparison.OrdinalIgnoreCase);
}
public static bool ExHasAllEquals(this string text, params string[] textArgs)
{
for (int index = 0; index < textArgs.Length; index++)
if (ExEquals(text, textArgs[index]) == false) return false;
return true;
}
public static bool ExHasEquals(this string text, params string[] textArgs)
{
for (int index = 0; index < textArgs.Length; index++)
if (ExEquals(text, textArgs[index])) return true;
return false;
}
public static bool ExHasNoEquals(this string text, params string[] textArgs)
{
return ExHasEquals(text, textArgs) == false;
}
public static bool ExHasNotAllEquals(this string text, params string[] textArgs)
{
for (int index = 0; index < textArgs.Length; index++)
if (ExEquals(text, textArgs[index])) return false;
return true;
}
/// <summary>
/// Reports the zero-based index of the first occurrence of the specified string
/// in the current System.String object using StringComparison.InvariantCultureIgnoreCase.
/// A parameter specifies the type of search to use for the specified string.
/// </summary>
/// <param name="fullText">
/// The string to search inside.
/// </param>
/// <param name="value">
/// The string to seek.
/// </param>
/// <returns>
/// The index position of the value parameter if that string is found, or -1 if it
/// is not. If value is System.String.Empty, the return value is 0.
/// </returns>
/// <exception cref="ArgumentNullException">
/// fullText or value is null.
/// </exception>
public static int ExIndexOf(this string fullText, string value)
{
return fullText.IndexOf(value, StringComparison.OrdinalIgnoreCase);
}
public static bool ExNotEquals(this string text, string textToCompare)
{
return ExEquals(text, textToCompare) == false;
}
#endregion Public Methods
}
Based on the existing answers and on the documentation of Contains method I would recommend the creation of the following extension which also takes care of the corner cases:
public static class VStringExtensions
{
public static bool Contains(this string source, string toCheck, StringComparison comp)
{
if (toCheck == null)
{
throw new ArgumentNullException(nameof(toCheck));
}
if (source.Equals(string.Empty))
{
return false;
}
if (toCheck.Equals(string.Empty))
{
return true;
}
return source.IndexOf(toCheck, comp) >= 0;
}
}
Simple way for newbie:
title.ToLower().Contains("string");//of course "string" is lowercase.

Regex for string without spacial characters or spaces [duplicate]

How do I check a string to make sure it contains numbers, letters, or space only?
In C# this is simple:
private bool HasSpecialChars(string yourString)
{
return yourString.Any(ch => ! char.IsLetterOrDigit(ch));
}
The easiest way it to use a regular expression:
Regular Expression for alphanumeric and underscores
Using regular expressions in .net:
http://www.regular-expressions.info/dotnet.html
MSDN Regular Expression
Regex.IsMatch
var regexItem = new Regex("^[a-zA-Z0-9 ]*$");
if(regexItem.IsMatch(YOUR_STRING)){..}
string s = #"$KUH% I*$)OFNlkfn$";
var withoutSpecial = new string(s.Where(c => Char.IsLetterOrDigit(c)
|| Char.IsWhiteSpace(c)).ToArray());
if (s != withoutSpecial)
{
Console.WriteLine("String contains special chars");
}
Try this way.
public static bool hasSpecialChar(string input)
{
string specialChar = #"\|!#$%&/()=?»«#£§€{}.-;'<>_,";
foreach (var item in specialChar)
{
if (input.Contains(item)) return true;
}
return false;
}
String test_string = "tesintg#$234524##";
if (System.Text.RegularExpressions.Regex.IsMatch(test_string, "^[a-zA-Z0-9\x20]+$"))
{
// Good-to-go
}
An example can be found here: http://ideone.com/B1HxA
If the list of acceptable characters is pretty small, you can use a regular expression like this:
Regex.IsMatch(items, "[a-z0-9 ]+", RegexOptions.IgnoreCase);
The regular expression used here looks for any character from a-z and 0-9 including a space (what's inside the square brackets []), that there is one or more of these characters (the + sign--you can use a * for 0 or more). The final option tells the regex parser to ignore case.
This will fail on anything that is not a letter, number, or space. To add more characters to the blessed list, add it inside the square brackets.
Use the regular Expression below in to validate a string to make sure it contains numbers, letters, or space only:
[a-zA-Z0-9 ]
You could do it with a bool. I've been learning recently and found I could do it this way. In this example, I'm checking a user's input to the console:
using System;
using System.Linq;
namespace CheckStringContent
{
class Program
{
static void Main(string[] args)
{
//Get a password to check
Console.WriteLine("Please input a Password: ");
string userPassword = Console.ReadLine();
//Check the string
bool symbolCheck = userPassword.Any(p => !char.IsLetterOrDigit(p));
//Write results to console
Console.WriteLine($"Symbols are present: {symbolCheck}");
}
}
}
This returns 'True' if special chars (symbolCheck) are present in the string, and 'False' if not present.
A great way using C# and Linq here:
public static bool HasSpecialCharacter(this string s)
{
foreach (var c in s)
{
if(!char.IsLetterOrDigit(c))
{
return true;
}
}
return false;
}
And access it like this:
myString.HasSpecialCharacter();
private bool isMatch(string strValue,string specialChars)
{
return specialChars.Where(x => strValue.Contains(x)).Any();
}
Create a method and call it hasSpecialChar with one parameter
and use foreach to check every single character in the textbox, add as many characters as you want in the array, in my case i just used ) and ( to prevent sql injection .
public void hasSpecialChar(string input)
{
char[] specialChar = {'(',')'};
foreach (char item in specialChar)
{
if (input.Contains(item)) MessageBox.Show("it contains");
}
}
in your button click evenement or you click btn double time like that :
private void button1_Click(object sender, EventArgs e)
{
hasSpecialChar(textbox1.Text);
}
While there are many ways to skin this cat, I prefer to wrap such code into reusable extension methods that make it trivial to do going forward. When using extension methods, you can also avoid RegEx as it is slower than a direct character check. I like using the extensions in the Extensions.cs NuGet package. It makes this check as simple as:
Add the [https://www.nuget.org/packages/Extensions.cs][1] package to your project.
Add "using Extensions;" to the top of your code.
"smith23#".IsAlphaNumeric() will return False whereas "smith23".IsAlphaNumeric() will return True. By default the .IsAlphaNumeric() method ignores spaces, but it can also be overridden such that "smith 23".IsAlphaNumeric(false) will return False since the space is not considered part of the alphabet.
Every other check in the rest of the code is simply MyString.IsAlphaNumeric().
Based on #prmph's answer, it can be even more simplified (omitting the variable, using overload resolution):
yourString.Any(char.IsLetterOrDigit);
No special characters or empty string except hyphen
^[a-zA-Z0-9-]+$

Case insensitive Contains in LINQ without making ToLower or ToUpper [duplicate]

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 (by which I am referring to the i18n issues that come with up- and down casing).
UPDATE
This question is ancient and since then I have realized I asked for a simple answer for a really vast and difficult topic if you care to investigate it fully.
For most cases, in mono-lingual, English code bases this answer will suffice. I'm suspecting because most people coming here fall in this category this is the most popular answer.
This answer however brings up the inherent problem that we can't compare text case insensitive until we know both texts are the same culture and we know what that culture is. This is maybe a less popular answer, but I think it is more correct and that's why I marked it as such.
You could use the String.IndexOf Method and pass StringComparison.OrdinalIgnoreCase as the type of search to use:
string title = "STRING";
bool contains = title.IndexOf("string", StringComparison.OrdinalIgnoreCase) >= 0;
Even better is defining a new extension method for string:
public static class StringExtensions
{
public static bool Contains(this string source, string toCheck, StringComparison comp)
{
return source?.IndexOf(toCheck, comp) >= 0;
}
}
Note, that null propagation ?. is available since C# 6.0 (VS 2015), for older versions use
if (source == null) return false;
return source.IndexOf(toCheck, comp) >= 0;
USAGE:
string title = "STRING";
bool contains = title.Contains("string", StringComparison.OrdinalIgnoreCase);
To test if the string paragraph contains the string word (thanks #QuarterMeister)
culture.CompareInfo.IndexOf(paragraph, word, CompareOptions.IgnoreCase) >= 0
Where culture is the instance of CultureInfo describing the language that the text is written in.
This solution is transparent about the definition of case-insensitivity, which is language dependent. For example, the English language uses the characters I and i for the upper and lower case versions of the ninth letter, whereas the Turkish language uses these characters for the eleventh and twelfth letters of its 29 letter-long alphabet. The Turkish upper case version of 'i' is the unfamiliar character 'İ'.
Thus the strings tin and TIN are the same word in English, but different words in Turkish. As I understand, one means 'spirit' and the other is an onomatopoeia word. (Turks, please correct me if I'm wrong, or suggest a better example)
To summarise, you can only answer the question 'are these two strings the same but in different cases' if you know what language the text is in. If you don't know, you'll have to take a punt. Given English's hegemony in software, you should probably resort to CultureInfo.InvariantCulture, because it will be wrong in familiar ways.
You can use IndexOf() like this:
string title = "STRING";
if (title.IndexOf("string", 0, StringComparison.OrdinalIgnoreCase) != -1)
{
// The string exists in the original
}
Since 0 (zero) can be an index, you check against -1.
Microsoft .NET Documentation:
The zero-based index position of the value parameter from the start of the current instance if that string is found, or -1 if it is not. If value is Empty, the return value is startIndex.
.NET Core 2.0+ (including .NET 5.0+)
.NET Core has had a pair of methods to deal with this since version 2.0 :
String.Contains(Char, StringComparison)
String.Contains(String, StringComparison)
Example:
"Test".Contains("test", System.StringComparison.CurrentCultureIgnoreCase);
It is now officially part of the .NET Standard 2.1, and therefore part of all the implementations of the Base Class Library that implement this version of the standard (or a higher one).
Alternative solution using Regex:
bool contains = Regex.IsMatch("StRiNG to search", Regex.Escape("string"), RegexOptions.IgnoreCase);
You could always just up or downcase the strings first.
string 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...
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:
public 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;
}
StringExtension class is the way forward, I've combined a couple of the posts above to give a complete code example:
public 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;
}
}
OrdinalIgnoreCase, CurrentCultureIgnoreCase or InvariantCultureIgnoreCase?
Since this is missing, here are some recommendations about when to use which one:
Dos
Use StringComparison.OrdinalIgnoreCase for comparisons
as your safe default for culture-agnostic string matching.
Use StringComparison.OrdinalIgnoreCase comparisons
for increased speed.
Use StringComparison.CurrentCulture-based string operations
when displaying the output to the user.
Switch current use of string operations based on the invariant
culture to use the non-linguistic StringComparison.Ordinal or StringComparison.OrdinalIgnoreCase when the comparison is
linguistically irrelevant (symbolic, for example).
Use ToUpperInvariant rather than ToLowerInvariant when
normalizing strings for comparison.
Don'ts
Use overloads for string operations that don't explicitly
or implicitly specify the string comparison mechanism.
Use StringComparison.InvariantCulture -based string
operations in most cases; one of the few exceptions would be
persisting linguistically meaningful but culturally-agnostic data.
Based on these rules you should use:
string title = "STRING";
if (title.IndexOf("string", 0, StringComparison.[YourDecision]) != -1)
{
// The string exists in the original
}
whereas [YourDecision] depends on the recommendations from above.
link of source: http://msdn.microsoft.com/en-us/library/ms973919.aspx
This is clean and simple.
Regex.IsMatch(file, fileNamestr, RegexOptions.IgnoreCase)
These are the easiest solutions.
By Index of
string title = "STRING";
if (title.IndexOf("string", 0, StringComparison.CurrentCultureIgnoreCase) != -1)
{
// contains
}
By Changing case
string title = "STRING";
bool contains = title.ToLower().Contains("string")
By Regex
Regex.IsMatch(title, "string", RegexOptions.IgnoreCase);
As simple and works
title.ToLower().Contains("String".ToLower())
Just like this:
string s="AbcdEf";
if(s.ToLower().Contains("def"))
{
Console.WriteLine("yes");
}
I know that this is not the C#, but in the framework (VB.NET) there is already such a function
Dim 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");
You can use a string comparison parameter (available from .NET Core 2.1 and above) String.Contains Method.
public bool Contains (string value, StringComparison comparisonType);
Example:
string title = "ASTRINGTOTEST";
title.Contains("string", StringComparison.InvariantCultureIgnoreCase);
The InStr method from the VisualBasic assembly is the best if you have a concern about internationalization (or you could reimplement it). Looking at in it dotNeetPeek shows that not only does it account for caps and lowercase, but also for kana type and full- vs. half-width characters (mostly relevant for Asian languages, although there are full-width versions of the Roman alphabet too). I'm skipping over some details, but check out the private method InternalInStrText:
private static int InternalInStrText(int lStartPos, string sSrc, string sFind)
{
int num = sSrc == null ? 0 : sSrc.Length;
if (lStartPos > num || num == 0)
return -1;
if (sFind == null || sFind.Length == 0)
return lStartPos;
else
return Utils.GetCultureInfo().CompareInfo.IndexOf(sSrc, sFind, lStartPos, CompareOptions.IgnoreCase | CompareOptions.IgnoreKanaType | CompareOptions.IgnoreWidth);
}
Use this:
string.Compare("string", "STRING", new System.Globalization.CultureInfo("en-US"), System.Globalization.CompareOptions.IgnoreCase);
This is quite similar to other example here, but I've decided to simplify enum to bool, primary because other alternatives are normally not needed. Here is my example:
public static class StringExtensions
{
public static bool Contains(this string source, string toCheck, bool bCaseInsensitive )
{
return source.IndexOf(toCheck, bCaseInsensitive ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal) >= 0;
}
}
And usage is something like:
if( "main String substring".Contains("SUBSTRING", true) )
....
Using a RegEx is a straight way to do this:
Regex.IsMatch(title, "string", RegexOptions.IgnoreCase);
Just to build on the answer here, you can create a string extension method to make this a little more user-friendly:
public static bool ContainsIgnoreCase(this string paragraph, string word)
{
return CultureInfo.CurrentCulture.CompareInfo.IndexOf(paragraph, word, CompareOptions.IgnoreCase) >= 0;
}
if you want to check if your passed string is in string then there is a simple method for that.
string yourStringForCheck= "abc";
string stringInWhichWeCheck= "Test abc abc";
bool isContained = stringInWhichWeCheck.ToLower().IndexOf(yourStringForCheck.ToLower()) > -1;
This boolean value will return if the string is contained or not
Similar to previous answers (using an extension method) but with two simple null checks (C# 6.0 and above):
public static bool ContainsIgnoreCase(this string source, string substring)
{
return source?.IndexOf(substring ?? "", StringComparison.OrdinalIgnoreCase) >= 0;
}
If source is null, return false (via null-propagation operator ?.)
If substring is null, treat as an empty string and return true (via null-coalescing operator ??)
The StringComparison can of course be sent as a parameter if needed.
The top-rated several answers are all good and correct in their own ways, I write here to add more information, context, and perspective.
For clarity, let us consider that string A contains string B if there is any subsequence of codepoints in A which is equal to B. If we accept this, the problem is reduced to the question of whether two strings are equal.
The question of when strings are equal has been considered in detail for many decades. Much of the present state of knowledge is encapsulated in SQL collations. Unicode normal forms are close to a proper subset of this. But there is more beyond even SQL collations.
For example, in SQL collations, you can be
Strictly binary sensitive - so that different Unicode normalisation forms (e.g. precombined or combining accents) compare differently.
For example, é can be represented as either U+00e9 (precombined) or U+0065 U+0301 (e with combining acute accent).
Are these the same or different?
Unicode normalised - In this case the above examples would be equal to each other, but not to É or e.
accent insensitive, (for e.g. Spanish, German, Swedish etc. text). In this case U+0065 = U+0065 U+0301 = U+00e9 = é = e
case and accent insensitive, so that (for e.g. Spanish, German, Swedish etc. text). In this case U+00e9 = U+0065 U+0301 = U+00c9 = U+0045 U+0301 = U+0049 = U+0065 = E = e = É = é
Kanatype sensitive or insensitive, i.e. you can consider Japanese Hiragana and Katakana as equivalent or different. The two syllabaries contain the same number of characters, organised and pronounced in the (mostly) the same way, but written differently and used for different purposes. For example katakana are used for loan words or foreign names, but hiragana are used for children's books, pronunciation guides (e.g. rubies), and where there is no kanji for a word (or perhaps where the writer does not know the kanji, or thinks the reader may not know it).
Full-width or half-width sensitive - Japanese encodings include two representations of some characters for historical reasons - they were displayed at different sizes.
Ligatures considered equivalent or not: See https://en.wikipedia.org/wiki/Ligature_(writing)
Is æ the same as ae or not? They have different Unicode encodings, as do accented characters, but unlike accented characters they also look different.
Which brings us to...
Arabic presentation form equivalence
Arabic writing has a culture of beautiful calligraphy, where particular sequences of adjacent letters have specific representations. Many of these have been encoded in the Unicode standard. I don't fully understand the rules, but they seem to me to be analogous to ligatures.
Other scripts and systems: I have no knowledge whatsoever or Kannada, Malayalam, Sinhala, Thai, Gujarati, Tibetan, or almost all of the tens or hundreds of scripts not mentioned. I assume they have similar issues for the programmer, and given the number of issues mentioned so far and for so few scripts, they probably also have additional issues the programmer ought to consider.
That gets us out of the "encoding" weeds.
Now we must enter the "meaning" weeds.
is Beijing equal to 北京? If not, is Bĕijīng equal to 北京? If not, why not? It is the Pinyin romanisation.
Is Peking equal to 北京? If not, why not? It is the Wade-Giles romanisation.
Is Beijing equal to Peking? If not, why not?
Why are you doing this anyway?
For example, if you want to know if it is possible that two strings (A and B) refer to the same geographical location, or same person, you might want to ask:
Could these strings be either Wade-Giles or Pinyin representations of a set of sequences of Chinese characters? If so, is there any overlap between the corresponding sets?
Could one of these strings be a Cyrillic transcription of a Chinese Character?
could one of these strings be a Cyrillic transliteration of the Pinyin romanisation?
Could one of these strings be a Cyrillic transliteration of a Pinyin romanisation of a Sinification of an English name?
Clearly these are difficult questions, which don't have firm answers, and in any case, the answer may be different according to the purpose of the question.
To finish with a concrete example.
If you are delivering a letter or parcel, clearly Beijing, Peking, Bĕijīng and 北京 are all equal. For that purpose, they are all equally good. No doubt the Chinese post-offices recognise many other options, such as Pékin in French, Pequim in Portuguese, Bắc Kinh in Vietnamese, and Бээжин in Mongolian.
Words do not have fixed meanings.
Words are tools we use to navigate the world, to accomplish our tasks, and to communicate with other people.
While it looks like it would be helpful if words like equality, Beijing, or meaning had fixed meanings, the sad fact is they do not.
Yet we seem to muddle along somehow.
TL;DR: If you are dealing with questions relating to reality, in all its nebulosity (cloudiness, uncertainty, lack of clear boundaries), there are basically three possible answers to every question:
Probably
Probably not
Maybe
if ("strcmpstring1".IndexOf(Convert.ToString("strcmpstring2"), StringComparison.CurrentCultureIgnoreCase) >= 0){return true;}else{return false;}
You can use string.indexof () function. This will be case insensitive
The trick here is to look for the string, ignoring case, but to keep it exactly the same (with the same case).
var s="Factory Reset";
var txt="reset";
int first = s.IndexOf(txt, StringComparison.InvariantCultureIgnoreCase) + txt.Length;
var subString = s.Substring(first - txt.Length, txt.Length);
Output is "Reset"
public static class StringExtension
{
#region Public Methods
public static bool ExContains(this string fullText, string value)
{
return ExIndexOf(fullText, value) > -1;
}
public static bool ExEquals(this string text, string textToCompare)
{
return text.Equals(textToCompare, StringComparison.OrdinalIgnoreCase);
}
public static bool ExHasAllEquals(this string text, params string[] textArgs)
{
for (int index = 0; index < textArgs.Length; index++)
if (ExEquals(text, textArgs[index]) == false) return false;
return true;
}
public static bool ExHasEquals(this string text, params string[] textArgs)
{
for (int index = 0; index < textArgs.Length; index++)
if (ExEquals(text, textArgs[index])) return true;
return false;
}
public static bool ExHasNoEquals(this string text, params string[] textArgs)
{
return ExHasEquals(text, textArgs) == false;
}
public static bool ExHasNotAllEquals(this string text, params string[] textArgs)
{
for (int index = 0; index < textArgs.Length; index++)
if (ExEquals(text, textArgs[index])) return false;
return true;
}
/// <summary>
/// Reports the zero-based index of the first occurrence of the specified string
/// in the current System.String object using StringComparison.InvariantCultureIgnoreCase.
/// A parameter specifies the type of search to use for the specified string.
/// </summary>
/// <param name="fullText">
/// The string to search inside.
/// </param>
/// <param name="value">
/// The string to seek.
/// </param>
/// <returns>
/// The index position of the value parameter if that string is found, or -1 if it
/// is not. If value is System.String.Empty, the return value is 0.
/// </returns>
/// <exception cref="ArgumentNullException">
/// fullText or value is null.
/// </exception>
public static int ExIndexOf(this string fullText, string value)
{
return fullText.IndexOf(value, StringComparison.OrdinalIgnoreCase);
}
public static bool ExNotEquals(this string text, string textToCompare)
{
return ExEquals(text, textToCompare) == false;
}
#endregion Public Methods
}
Based on the existing answers and on the documentation of Contains method I would recommend the creation of the following extension which also takes care of the corner cases:
public static class VStringExtensions
{
public static bool Contains(this string source, string toCheck, StringComparison comp)
{
if (toCheck == null)
{
throw new ArgumentNullException(nameof(toCheck));
}
if (source.Equals(string.Empty))
{
return false;
}
if (toCheck.Equals(string.Empty))
{
return true;
}
return source.IndexOf(toCheck, comp) >= 0;
}
}
Simple way for newbie:
title.ToLower().Contains("string");//of course "string" is lowercase.

Case insensitive 'Contains(string)'

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 (by which I am referring to the i18n issues that come with up- and down casing).
UPDATE
This question is ancient and since then I have realized I asked for a simple answer for a really vast and difficult topic if you care to investigate it fully.
For most cases, in mono-lingual, English code bases this answer will suffice. I'm suspecting because most people coming here fall in this category this is the most popular answer.
This answer however brings up the inherent problem that we can't compare text case insensitive until we know both texts are the same culture and we know what that culture is. This is maybe a less popular answer, but I think it is more correct and that's why I marked it as such.
You could use the String.IndexOf Method and pass StringComparison.OrdinalIgnoreCase as the type of search to use:
string title = "STRING";
bool contains = title.IndexOf("string", StringComparison.OrdinalIgnoreCase) >= 0;
Even better is defining a new extension method for string:
public static class StringExtensions
{
public static bool Contains(this string source, string toCheck, StringComparison comp)
{
return source?.IndexOf(toCheck, comp) >= 0;
}
}
Note, that null propagation ?. is available since C# 6.0 (VS 2015), for older versions use
if (source == null) return false;
return source.IndexOf(toCheck, comp) >= 0;
USAGE:
string title = "STRING";
bool contains = title.Contains("string", StringComparison.OrdinalIgnoreCase);
To test if the string paragraph contains the string word (thanks #QuarterMeister)
culture.CompareInfo.IndexOf(paragraph, word, CompareOptions.IgnoreCase) >= 0
Where culture is the instance of CultureInfo describing the language that the text is written in.
This solution is transparent about the definition of case-insensitivity, which is language dependent. For example, the English language uses the characters I and i for the upper and lower case versions of the ninth letter, whereas the Turkish language uses these characters for the eleventh and twelfth letters of its 29 letter-long alphabet. The Turkish upper case version of 'i' is the unfamiliar character 'İ'.
Thus the strings tin and TIN are the same word in English, but different words in Turkish. As I understand, one means 'spirit' and the other is an onomatopoeia word. (Turks, please correct me if I'm wrong, or suggest a better example)
To summarise, you can only answer the question 'are these two strings the same but in different cases' if you know what language the text is in. If you don't know, you'll have to take a punt. Given English's hegemony in software, you should probably resort to CultureInfo.InvariantCulture, because it will be wrong in familiar ways.
You can use IndexOf() like this:
string title = "STRING";
if (title.IndexOf("string", 0, StringComparison.OrdinalIgnoreCase) != -1)
{
// The string exists in the original
}
Since 0 (zero) can be an index, you check against -1.
Microsoft .NET Documentation:
The zero-based index position of the value parameter from the start of the current instance if that string is found, or -1 if it is not. If value is Empty, the return value is startIndex.
.NET Core 2.0+ (including .NET 5.0+)
.NET Core has had a pair of methods to deal with this since version 2.0 :
String.Contains(Char, StringComparison)
String.Contains(String, StringComparison)
Example:
"Test".Contains("test", System.StringComparison.CurrentCultureIgnoreCase);
It is now officially part of the .NET Standard 2.1, and therefore part of all the implementations of the Base Class Library that implement this version of the standard (or a higher one).
Alternative solution using Regex:
bool contains = Regex.IsMatch("StRiNG to search", Regex.Escape("string"), RegexOptions.IgnoreCase);
You could always just up or downcase the strings first.
string 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...
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:
public 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;
}
StringExtension class is the way forward, I've combined a couple of the posts above to give a complete code example:
public 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;
}
}
OrdinalIgnoreCase, CurrentCultureIgnoreCase or InvariantCultureIgnoreCase?
Since this is missing, here are some recommendations about when to use which one:
Dos
Use StringComparison.OrdinalIgnoreCase for comparisons
as your safe default for culture-agnostic string matching.
Use StringComparison.OrdinalIgnoreCase comparisons
for increased speed.
Use StringComparison.CurrentCulture-based string operations
when displaying the output to the user.
Switch current use of string operations based on the invariant
culture to use the non-linguistic StringComparison.Ordinal or StringComparison.OrdinalIgnoreCase when the comparison is
linguistically irrelevant (symbolic, for example).
Use ToUpperInvariant rather than ToLowerInvariant when
normalizing strings for comparison.
Don'ts
Use overloads for string operations that don't explicitly
or implicitly specify the string comparison mechanism.
Use StringComparison.InvariantCulture -based string
operations in most cases; one of the few exceptions would be
persisting linguistically meaningful but culturally-agnostic data.
Based on these rules you should use:
string title = "STRING";
if (title.IndexOf("string", 0, StringComparison.[YourDecision]) != -1)
{
// The string exists in the original
}
whereas [YourDecision] depends on the recommendations from above.
link of source: http://msdn.microsoft.com/en-us/library/ms973919.aspx
This is clean and simple.
Regex.IsMatch(file, fileNamestr, RegexOptions.IgnoreCase)
These are the easiest solutions.
By Index of
string title = "STRING";
if (title.IndexOf("string", 0, StringComparison.CurrentCultureIgnoreCase) != -1)
{
// contains
}
By Changing case
string title = "STRING";
bool contains = title.ToLower().Contains("string")
By Regex
Regex.IsMatch(title, "string", RegexOptions.IgnoreCase);
As simple and works
title.ToLower().Contains("String".ToLower())
Just like this:
string s="AbcdEf";
if(s.ToLower().Contains("def"))
{
Console.WriteLine("yes");
}
I know that this is not the C#, but in the framework (VB.NET) there is already such a function
Dim 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");
You can use a string comparison parameter (available from .NET Core 2.1 and above) String.Contains Method.
public bool Contains (string value, StringComparison comparisonType);
Example:
string title = "ASTRINGTOTEST";
title.Contains("string", StringComparison.InvariantCultureIgnoreCase);
The InStr method from the VisualBasic assembly is the best if you have a concern about internationalization (or you could reimplement it). Looking at in it dotNeetPeek shows that not only does it account for caps and lowercase, but also for kana type and full- vs. half-width characters (mostly relevant for Asian languages, although there are full-width versions of the Roman alphabet too). I'm skipping over some details, but check out the private method InternalInStrText:
private static int InternalInStrText(int lStartPos, string sSrc, string sFind)
{
int num = sSrc == null ? 0 : sSrc.Length;
if (lStartPos > num || num == 0)
return -1;
if (sFind == null || sFind.Length == 0)
return lStartPos;
else
return Utils.GetCultureInfo().CompareInfo.IndexOf(sSrc, sFind, lStartPos, CompareOptions.IgnoreCase | CompareOptions.IgnoreKanaType | CompareOptions.IgnoreWidth);
}
Use this:
string.Compare("string", "STRING", new System.Globalization.CultureInfo("en-US"), System.Globalization.CompareOptions.IgnoreCase);
This is quite similar to other example here, but I've decided to simplify enum to bool, primary because other alternatives are normally not needed. Here is my example:
public static class StringExtensions
{
public static bool Contains(this string source, string toCheck, bool bCaseInsensitive )
{
return source.IndexOf(toCheck, bCaseInsensitive ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal) >= 0;
}
}
And usage is something like:
if( "main String substring".Contains("SUBSTRING", true) )
....
Using a RegEx is a straight way to do this:
Regex.IsMatch(title, "string", RegexOptions.IgnoreCase);
Just to build on the answer here, you can create a string extension method to make this a little more user-friendly:
public static bool ContainsIgnoreCase(this string paragraph, string word)
{
return CultureInfo.CurrentCulture.CompareInfo.IndexOf(paragraph, word, CompareOptions.IgnoreCase) >= 0;
}
if you want to check if your passed string is in string then there is a simple method for that.
string yourStringForCheck= "abc";
string stringInWhichWeCheck= "Test abc abc";
bool isContained = stringInWhichWeCheck.ToLower().IndexOf(yourStringForCheck.ToLower()) > -1;
This boolean value will return if the string is contained or not
Similar to previous answers (using an extension method) but with two simple null checks (C# 6.0 and above):
public static bool ContainsIgnoreCase(this string source, string substring)
{
return source?.IndexOf(substring ?? "", StringComparison.OrdinalIgnoreCase) >= 0;
}
If source is null, return false (via null-propagation operator ?.)
If substring is null, treat as an empty string and return true (via null-coalescing operator ??)
The StringComparison can of course be sent as a parameter if needed.
The top-rated several answers are all good and correct in their own ways, I write here to add more information, context, and perspective.
For clarity, let us consider that string A contains string B if there is any subsequence of codepoints in A which is equal to B. If we accept this, the problem is reduced to the question of whether two strings are equal.
The question of when strings are equal has been considered in detail for many decades. Much of the present state of knowledge is encapsulated in SQL collations. Unicode normal forms are close to a proper subset of this. But there is more beyond even SQL collations.
For example, in SQL collations, you can be
Strictly binary sensitive - so that different Unicode normalisation forms (e.g. precombined or combining accents) compare differently.
For example, é can be represented as either U+00e9 (precombined) or U+0065 U+0301 (e with combining acute accent).
Are these the same or different?
Unicode normalised - In this case the above examples would be equal to each other, but not to É or e.
accent insensitive, (for e.g. Spanish, German, Swedish etc. text). In this case U+0065 = U+0065 U+0301 = U+00e9 = é = e
case and accent insensitive, so that (for e.g. Spanish, German, Swedish etc. text). In this case U+00e9 = U+0065 U+0301 = U+00c9 = U+0045 U+0301 = U+0049 = U+0065 = E = e = É = é
Kanatype sensitive or insensitive, i.e. you can consider Japanese Hiragana and Katakana as equivalent or different. The two syllabaries contain the same number of characters, organised and pronounced in the (mostly) the same way, but written differently and used for different purposes. For example katakana are used for loan words or foreign names, but hiragana are used for children's books, pronunciation guides (e.g. rubies), and where there is no kanji for a word (or perhaps where the writer does not know the kanji, or thinks the reader may not know it).
Full-width or half-width sensitive - Japanese encodings include two representations of some characters for historical reasons - they were displayed at different sizes.
Ligatures considered equivalent or not: See https://en.wikipedia.org/wiki/Ligature_(writing)
Is æ the same as ae or not? They have different Unicode encodings, as do accented characters, but unlike accented characters they also look different.
Which brings us to...
Arabic presentation form equivalence
Arabic writing has a culture of beautiful calligraphy, where particular sequences of adjacent letters have specific representations. Many of these have been encoded in the Unicode standard. I don't fully understand the rules, but they seem to me to be analogous to ligatures.
Other scripts and systems: I have no knowledge whatsoever or Kannada, Malayalam, Sinhala, Thai, Gujarati, Tibetan, or almost all of the tens or hundreds of scripts not mentioned. I assume they have similar issues for the programmer, and given the number of issues mentioned so far and for so few scripts, they probably also have additional issues the programmer ought to consider.
That gets us out of the "encoding" weeds.
Now we must enter the "meaning" weeds.
is Beijing equal to 北京? If not, is Bĕijīng equal to 北京? If not, why not? It is the Pinyin romanisation.
Is Peking equal to 北京? If not, why not? It is the Wade-Giles romanisation.
Is Beijing equal to Peking? If not, why not?
Why are you doing this anyway?
For example, if you want to know if it is possible that two strings (A and B) refer to the same geographical location, or same person, you might want to ask:
Could these strings be either Wade-Giles or Pinyin representations of a set of sequences of Chinese characters? If so, is there any overlap between the corresponding sets?
Could one of these strings be a Cyrillic transcription of a Chinese Character?
could one of these strings be a Cyrillic transliteration of the Pinyin romanisation?
Could one of these strings be a Cyrillic transliteration of a Pinyin romanisation of a Sinification of an English name?
Clearly these are difficult questions, which don't have firm answers, and in any case, the answer may be different according to the purpose of the question.
To finish with a concrete example.
If you are delivering a letter or parcel, clearly Beijing, Peking, Bĕijīng and 北京 are all equal. For that purpose, they are all equally good. No doubt the Chinese post-offices recognise many other options, such as Pékin in French, Pequim in Portuguese, Bắc Kinh in Vietnamese, and Бээжин in Mongolian.
Words do not have fixed meanings.
Words are tools we use to navigate the world, to accomplish our tasks, and to communicate with other people.
While it looks like it would be helpful if words like equality, Beijing, or meaning had fixed meanings, the sad fact is they do not.
Yet we seem to muddle along somehow.
TL;DR: If you are dealing with questions relating to reality, in all its nebulosity (cloudiness, uncertainty, lack of clear boundaries), there are basically three possible answers to every question:
Probably
Probably not
Maybe
if ("strcmpstring1".IndexOf(Convert.ToString("strcmpstring2"), StringComparison.CurrentCultureIgnoreCase) >= 0){return true;}else{return false;}
You can use string.indexof () function. This will be case insensitive
The trick here is to look for the string, ignoring case, but to keep it exactly the same (with the same case).
var s="Factory Reset";
var txt="reset";
int first = s.IndexOf(txt, StringComparison.InvariantCultureIgnoreCase) + txt.Length;
var subString = s.Substring(first - txt.Length, txt.Length);
Output is "Reset"
public static class StringExtension
{
#region Public Methods
public static bool ExContains(this string fullText, string value)
{
return ExIndexOf(fullText, value) > -1;
}
public static bool ExEquals(this string text, string textToCompare)
{
return text.Equals(textToCompare, StringComparison.OrdinalIgnoreCase);
}
public static bool ExHasAllEquals(this string text, params string[] textArgs)
{
for (int index = 0; index < textArgs.Length; index++)
if (ExEquals(text, textArgs[index]) == false) return false;
return true;
}
public static bool ExHasEquals(this string text, params string[] textArgs)
{
for (int index = 0; index < textArgs.Length; index++)
if (ExEquals(text, textArgs[index])) return true;
return false;
}
public static bool ExHasNoEquals(this string text, params string[] textArgs)
{
return ExHasEquals(text, textArgs) == false;
}
public static bool ExHasNotAllEquals(this string text, params string[] textArgs)
{
for (int index = 0; index < textArgs.Length; index++)
if (ExEquals(text, textArgs[index])) return false;
return true;
}
/// <summary>
/// Reports the zero-based index of the first occurrence of the specified string
/// in the current System.String object using StringComparison.InvariantCultureIgnoreCase.
/// A parameter specifies the type of search to use for the specified string.
/// </summary>
/// <param name="fullText">
/// The string to search inside.
/// </param>
/// <param name="value">
/// The string to seek.
/// </param>
/// <returns>
/// The index position of the value parameter if that string is found, or -1 if it
/// is not. If value is System.String.Empty, the return value is 0.
/// </returns>
/// <exception cref="ArgumentNullException">
/// fullText or value is null.
/// </exception>
public static int ExIndexOf(this string fullText, string value)
{
return fullText.IndexOf(value, StringComparison.OrdinalIgnoreCase);
}
public static bool ExNotEquals(this string text, string textToCompare)
{
return ExEquals(text, textToCompare) == false;
}
#endregion Public Methods
}
Based on the existing answers and on the documentation of Contains method I would recommend the creation of the following extension which also takes care of the corner cases:
public static class VStringExtensions
{
public static bool Contains(this string source, string toCheck, StringComparison comp)
{
if (toCheck == null)
{
throw new ArgumentNullException(nameof(toCheck));
}
if (source.Equals(string.Empty))
{
return false;
}
if (toCheck.Equals(string.Empty))
{
return true;
}
return source.IndexOf(toCheck, comp) >= 0;
}
}
Simple way for newbie:
title.ToLower().Contains("string");//of course "string" is lowercase.

Categories