How to compare character ignoring case in primitive types
The Character
class of Java API has various functions you can use.
You can convert your char to lowercase at both sides:
Character.toLowerCase(name1.charAt(i)) == Character.toLowerCase(name2.charAt(j))
There are also a methods you can use to verify if the letter is uppercase or lowercase:
Character.isUpperCase('P')
Character.isLowerCase('P')
combining charAt and IgnoreCase?
A neat trick for case insensitivity is to simply convert to lowercase before you compare. The class Character
contains a number of useful functions for manipulating characters, so you can do this:
} while (Character.toLowerCase(newGame.charAt(0)) == 'y');
character array comparison ignoring case
equalsIgnoreCase
is a String
method. You can't apply it on char
s.
You can write
if (!Character.toLowerCase(student[i]) == Character.toLowerCase(correct[i]))
or
if (!Character.toUpperCase(student[i]) == Character.toUpperCase(correct[i]))
How to compare two characters without case sensitivity in C?
You can use low case for both chars, for example by using tolower function:
if (tolower(str1[i])==tolower(str2[j])) printf("Equal");
Also keep in mind: tolower does not work for multibyte char. So for those chars you should use other function
How to compare character ignoring case in primitive types
The Character
class of Java API has various functions you can use.
You can convert your char to lowercase at both sides:
Character.toLowerCase(name1.charAt(i)) == Character.toLowerCase(name2.charAt(j))
There are also a methods you can use to verify if the letter is uppercase or lowercase:
Character.isUpperCase('P')
Character.isLowerCase('P')
How do I change a char using toUpperCase() in Java
You could use java.lang.Character
, there's a method called toUpperCase
, that's the best way to get a char in upper case.
You could use: Character.toUpperCase(rating) == 'E'
...
Compare user input (char) with other char
You can use either toUpperCase(char ch)
or toLowerCase(char ch)
1 to convert the user input and compare like below:
if ( Character.toLowerCase(inlet) == 'a' ){
}
OR
if ( Character.toUpperCase(inlet) == 'A' ){
}
Ignoring uppercase and lowercase on char when comparing
Since you are comparing on primitive char,
Character.toLowerCase(sentence.charAt(i))=='a'
Character.toUpperCase(sentence.charAt(i))=='A'
should already be the best way for your case in Java.
But if you are comparing on Stirng
sentence.substring(i,i+1).equalsIgnoreCase("a")
will be more direct , but a little bit harder to read.
If you have a String type inside an array or list, calling
s.equalsIgnoreCase("a")
will be much better. Please note that you are now comparing with "a" not 'a'.
If you are using StringBuilder
/StringBuffer
, you can call toString()
, and then use the same method.
sb.toString().substring(i,i+1).equalsIgnoreCase("a");
Related Topics
How to Convert a String to Another Locale
How to Call Subclasses' Methods on a Superclass Object
Why Equal Operator Works for Integer Value Until 128 Number
Where Is the Javabean Property Naming Convention Defined
<Form Action="/Sampleservlet" Giving Me Exception
How to Change the Dock Icon of a Java Program
Create a New Line in Java's Filewriter
How to Make Notepad to Save Text in Utf-8 Without the Bom
Using Jasperreports with a Relative Path
Date Format Parse Exception - "Eee Mmm Dd Hh:Mm:Ss Z Yyyy"
How to Display a Svg Byte Array as an Image in a Jasperreport