Indexof Case Sensitive

indexOf Case Sensitive?

The indexOf() methods are all case-sensitive. You can make them (roughly, in a broken way, but working for plenty of cases) case-insensitive by converting your strings to upper/lower case beforehand:

s1 = s1.toLowerCase(Locale.US);
s2 = s2.toLowerCase(Locale.US);
s1.indexOf(s2);

how to make indexof case insensitive in java

The safest way to do it would be:

content.toLowerCase().indexOf(searchTerm.toLowerCase()), this way you cover 2 possible edge cases when the content may be in lower case and the search term would be in upper case or both would be upper case.

How do I make Array.indexOf() case insensitive?

Easy way would be to have a temporary array that contains all the names in uppercase. Then you can compare the user input. So your code could become somthing like this:

function delName() {
var dnameVal = document.getElementById('delname').value;
var upperCaseNames = names.map(function(value) {
return value.toUpperCase();
});
var pos = upperCaseNames.indexOf(dnameVal.toUpperCase());

if(pos === -1) {
alert("Not a valid name");
}
else {
names.splice(pos, 1);
document.getElementById('canidates').innerHTML =
"<strong>List of Canidates:</strong> " + names.join(" | ");
}
}

Hope this helps solve your problem.

Arraylist.indexOf() case sensitive

If you want all names to start with a capital letter you can only store them in this way, and then print in this way. The following code will help:

private String capitalize(String name) {
String s1 = name.substring(0, 1).toUpperCase();
String nameCapitalized = s1 + name.substring(1).toLowerCase();
return nameCapitalized;
}

Use this method befor storing anything to your array and then ask for first occurence of capitalize(name);

In your case:

case 2: {
System.out.println("Enter the contact name you want to edit");
temp=s.next();
int z=name.indexOf(capitalize(temp));
if(z!=-1)
{
System.out.println("Edit to?");
temp=s.next();
name.set(z, capitalize(temp));
System.out.println("Name edited to "+capitalize(temp));
}
else
System.out.println("Name not found");
break;
}

This approach will let you keep all names in the same convention - names starting with a capital letter.

How do I use indexOf() case insensitively?

You can Use Overloaded Method

IndexOf("punctuation", StringComparison.OrdinalIgnoreCase);

Eg.

List<string> fnColArr = new List<string>() 
{ "Punctuation", "period", "Space", "and", "yes" };

foreach (string item in fnColArr)
{
if (item.IndexOf("puNctuation", StringComparison.OrdinalIgnoreCase) >= 0)
{
Console.WriteLine("match");

}
}

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

JavaScript indexOf to ignore Case

You can use regex with a case-insensitive modifier - admittedly not necessarily as fast as indexOf.

var noPic = largeSrc.search(/nopic/i);

Non-case sensitive replacement for indexOf()

You need to normalize the case of both strings, then do indexOf.

function indexOfCaseInsenstive(a, b) {
a = a.toLowerCase();
b = b.toLowerCase();

return a.indexOf(b);
}

Typescript: How to make array.indexOf case insensitive search

You should consider making the forbidden names either complete upperCase or complete lowerCase. And then do the needful when comparing;

Something like :

forbiddenUsernames = ['chris', 'ashutosh'];

ngOnInit() {
this.signupForm = new FormGroup({
'username': new FormControl(null, [Validators.required, this.forbiddenNames.bind(this)])
});
}

// validator is just a function, its our own validator
forbiddenNames(control: FormControl): {
[s: string]: boolean
} {
if (control.value && this.forbiddenUsernames.indexOf(control.value.toLowerCase()) !== -1) { // "-1" - did not find a match
return {
'nameIsForbidden': true
};
}
}

Update:

If your list of forbidden names is coming from a database, you can simply apply a map on the list to get the lowercased list.

forbiddenNames = [
'John',
'JANE',
'jacob',
'SaM',
'JeReMy',
'sIdDhArTh'
];

forbiddenNames.map(name => name.toLowerCase());


Related Topics



Leave a reply



Submit