Assume you had a string "ACcwerwervwvrwBq^2424 /.* DffGZ..'B
". How would you only keep certain characters like A,B,C,D
and remove the rest?
string.replaceAll
seems to work if I know what characters to remove, but I want to remove all characters except A,B,C,D
. Putting every character in there except those 4 seems pretty tedious, what the easier way?
I want the output in the above case to be "ACBDB
".
You would use regex something like:
str.replaceAll("[^ABCD]", "");
Should do it
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