I have a variable length string, I just want to detect whether this string contains several characters. For example:
"sadsdd$sss^dee~"
I want to detect whether this string contains ANY of the following: $
^
~
. How can I do that using Java string.matches
?
"sadsdd$sss^dee~".matches("[^+$+~+]");
Use a pattern and a matcher for that:
Pattern pattern = Pattern.compile("[$~^]");
Matcher matcher = pattern.matcher(input);
if (matcher.find()) {
// contains special characters
} else {
// doesn't contain special characters
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With