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.
Contains case insensitive
Add .toUpperCase()
after referrer
. This method turns the string into an upper case string. Then, use .indexOf()
using RAL
instead of Ral
.
if (referrer.toUpperCase().indexOf("RAL") === -1) {
The same can also be achieved using a Regular Expression (especially useful when you want to test against dynamic patterns):
if (!/Ral/i.test(referrer)) {
// ^i = Ignore case flag for RegExp
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.
Make String.Contains() case insensitive
If x.IndexOf(y, StringComparison.CurrentCultureIgnoreCase) >= 0 Then
'do nothing
End If
c# contains case insensitive search
You can use
ToLower()
function.ToLower
changes strings to be all lowercase. It converts an entire string—without changing letters that are already lowercased or digits. It copies a string and returns a reference to the new string. So it is always better option to declarecriteria.Author.ToLower()
outside the query.string lowerAuthor = criteria.Author.ToLower();
returnData = returnData.Where
(x => x.Author.ToLower().Contains(lowerAuthor));You could also use
IndexOf
overload with theStringComparison
enum. It would give you better performance thanToLower()
. The signature of this overload is:int string.IndexOf(string value, StringComparison comparisonType);
returnData = returnData.Where
(x => x.Author.IndexOf(criteria.Author, StringComparison.CurrentCultureIgnoreCase) != -1);
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 );
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;
}
How do I make contains case-insensitive in ef core 2?
starting from version 2.1 of the EF Core, you can use HasConversion(). But the information in the database will be stored in lowercase:
builder.Property(it => it.Email).HasConversion(v => v.ToLowerInvariant(), v => v);
I solved a similar problem. This change solved all my problems.
Related Topics
Uncaught Syntaxerror: Unexpected Token with JSON.Parse
How to Check If the Array of Objects Have Duplicate Property Values
Method Overloading in JavaScript
Jquery Post() with Serialize and Extra Data
Avoid Window Jump to Top When Clicking #-Links
How to Bind the HTML <Title> Content in Vuejs
How to Rotate a Single Object on an HTML 5 Canvas
Creating Drop Down Menu on Click CSS
Best Way to Execute Js Only on Specific Page
How to Make a <Div> Move Up and Down When I'm Scrolling the Page
What's the Best Way to Retry an Ajax Request on Failure Using Jquery
How to Copy Text from a Div to Clipboard
How to Loop Through All the Elements Returned from Getelementsbytagname
Window.Open and Pass Parameters by Post Method
How to Apply CSS to the Elements Within an Iframe
Webpack File-Loader Outputs [Object Module]