I am trying to do something like this but I doesnt seem to work
String test = input.matches("[^a-zA-Z\\s]");
I get an error saying incompatible types
Is there any way fixing this or any other methods?
EDIT
This is what I’m trying to achieve
Lets say i have this string
String full = " This i$ an aaasdAr yeEeeAs oofo qwdsgy XY9 ";
and i need to have the output as $9
this works but its a bit too messy
for (int index = 0; index <= input.length() - 1; index ++)
{
if(String.valueOf(input.charAt(index)).matches("[^a-zA-Z\\s]"))
{
System.out.print(String.valueOf(input.charAt(index)));
}
}
To solve your specific problem, you can use
String.replaceAll, which takes a replacement regex:This replaces everything that matches the regex with the empty string, and it prints
$9.