I am was making an if statement when I needed to check whether an unknown char is of any character set. Predefined or Custom.
I could just make many or statements but it would get messy. So is there any regex present to do this. I used a charAt(); function to get the last digit of it.I needed to check whether it is a digit or not. I currently need for digits only(0-9) but I would need more too.
This is my IF Statement
.....
if(mytext.equals("(") && (mytext2.charAt(text.length()-1)=='') {
......
}
.......
Please Help
You can use the Set interface like:
Set<Character> validChars = new HashSet<>(Arrays.asList('a', 'X, '1'));
if (validChars.contains(unknownChar)) {
...
for example. That gives you theoretically the best performance, although that won't matter much here.
Alternatively, you can collect your valid chars in a "string", as indicated by the comment. You then use thatString.indexOf(unknownChar)
, and if the method does return an index equal/greater than 0, you know "it is valid", too.
The third option would be to look into regular expressions, but for a newbie, I suggest either option 1 or 2.