0 votes

Replacing characters in Java

I want to replace the java characters in a word:

Hol?a por -> Hol a 

private String escapaCaracter(String valor) {

String valorOk = " ";
try {
      valorOk = valor
                .replaceAll("/", "-")
                .replaceAll("\\\\", " ")
                .replaceAll("¿", " ")
                .replaceAll("?", " ")
                .replaceAll("#", " ")
                .replaceAll("<", " ")
                .replaceAll(">", " ")
                .replaceAll("[", " ")
                .replaceAll("]", " ")
                .replaceAll("{", " ")
                .replaceAll("}", " ");
} catch (Exception e) {
    if (s_log.isLoggable(Level.FINEST)) {
        s_log.log(Level.FINEST, "Ha fallado el escapaCaracter para el valor: " + valor);
    }
}

    return valorOk;
}

The problem is that even if it does not contain any of these characters, it fails ->

Value I was testing with -> pro

error ->

java.util.regex.PatternSyntaxException: Dangling meta character '?' near index 0
?
^

These characters correspond to some values that when a url is assembled could cause failures, I say this because of if anyone knows of more characters that could give failures, i.e. :

parameter1= hol/a ;

When mounting the url would appear -> http://...hol/a/param2... A blank space is intended to be left -> value = hol/a ; -> http://...hol a/param2...

HolaDevs.com

HolaDevs is an online community of programmers and software lovers.
You can check other people responses or create a new question if you don't find a solution

Powered by:

X