String Contains - Ignore Case

How to check if a String contains another String in a case insensitive manner in Java?

Yes, contains is case sensitive. You can use java.util.regex.Pattern with the CASE_INSENSITIVE flag for case insensitive matching:

Pattern.compile(Pattern.quote(wantedStr), Pattern.CASE_INSENSITIVE).matcher(source).find();

EDIT: If s2 contains regex special characters (of which there are many) it's important to quote it first. I've corrected my answer since it is the first one people will see, but vote up Matt Quail's since he pointed this out.

String contains - ignore case

You can use

org.apache.commons.lang3.StringUtils.containsIgnoreCase(CharSequence str,
CharSequence searchStr);

Checks if CharSequence contains a search CharSequence irrespective of
case, handling null. Case-insensitivity is defined as by
String.equalsIgnoreCase(String).

A null CharSequence will return false.

This one will be better than regex as regex is always expensive in terms of performance.

For official doc, refer to : StringUtils.containsIgnoreCase

Update :

If you are among the ones who

  • don't want to use Apache commons library
  • don't want to go with the expensive regex/Pattern based solutions,
  • don't want to create additional string object by using toLowerCase,

you can implement your own custom containsIgnoreCase using java.lang.String.regionMatches

public boolean regionMatches(boolean ignoreCase,
int toffset,
String other,
int ooffset,
int len)

ignoreCase : if true, ignores case when comparing characters.

public static boolean containsIgnoreCase(String str, String searchStr)     {
if(str == null || searchStr == null) return false;

final int length = searchStr.length();
if (length == 0)
return true;

for (int i = str.length() - length; i >= 0; i--) {
if (str.regionMatches(true, i, searchStr, 0, length))
return true;
}
return false;
}

Case insensitive 'Contains(string)'

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.

String.Replace ignoring case

You could use a Regex and perform a case insensitive replace:

class Program
{
static void Main()
{
string input = "hello WoRlD";
string result =
Regex.Replace(input, "world", "csharp", RegexOptions.IgnoreCase);
Console.WriteLine(result); // prints "hello csharp"
}
}

Ignore case for 'contains' for a string in Java

You need to convert both the strings to the same case before using contains

s.toLowerCase().contains("ABCD".toLowerCase());

Option to ignore case with .contains method?

I'm guessing you mean ignoring case when searching in a string?

I don't know any, but you could try to convert the string to search into either to lower or to upper case, then search.

// s is the String to search into, and seq the sequence you are searching for.
bool doesContain = s.toLowerCase().contains(seq);

Edit:
As Ryan Schipper suggested, you can also (and probably would be better off) do seq.toLowerCase(), depending on your situation.

How to match a substring in a string, ignoring case

If you don't want to use str.lower(), you can use a regular expression:

import re

if re.search('mandy', 'Mandy Pande', re.IGNORECASE):
# Is True

javascript includes() case insensitive

You can create a RegExp from filterstrings first

var filterstrings = ['firststring','secondstring','thridstring'];
var regex = new RegExp( filterstrings.join( "|" ), "i");

then test if the passedinstring is there

var isAvailable = regex.test( passedinstring ); 

How to ignorecase when using string.text.contains?

According to Microsoft you can do case-insensitive searches in strings with IndexOf instead of Contains. So when the result of the IndexOf method returns a value greater than -1, it means the second string is a substring of the first one.

Dim myhousestring As String = "My house is cold"
If txt.Text.IndexOf(myhousestring, 0, StringComparison.CurrentCultureIgnoreCase) > -1 Then
Messagebox.Show("Found it")
End If

You can also use other case-insensitive variants of StringComparison.



Related Topics



Leave a reply



Submit