Celiiine Celiiine - 4 months ago 5x
Java Question

Replace all characters bigger than f

I use normally this command to replace characters in one String

myString.replace("f", "a").trim()

but this time I want to create a Hex String so I want to replace all characters that are bigger than
with the character
Is it possible to adapt this command ?


If you have an upper bounding character (I'll use z as an example), you could use a regular expression with replaceAll:

myString = myString.trim().replaceAll("[g-z]", "a");

The regular expression [g-z] means "any character g through z inclusive", see Pattern for details.

You may want to create the regular expression explicitly rather than relying on replaceAll's default version, if you want case-insensitivity for instance:

myString = Pattern.compile("[g-z]", Pattern.CASE_INSENSITIVE)