The first parameter of the replaceAll
method is a regular expression. So there are two alternatives:
-
Use the correct regular expression. With "\\"
in the source code, each \
is an escape sequence for the \
character, so that the content of the string that the compiler will see will be \
. As a regular expression, this is the escape sequence for the character \
alone.
-
Do not use regular expressions and use the method replace
instead of replaceAll
.
Here's a test that uses both approaches:
class Teste {
public static void main(String[] args) {
String teste1 = "a\b\c\d\e";
System.out.println(teste1);
String teste2 = teste1.replace("\", "");
System.out.println(teste2);
String teste3 = teste1.replaceAll("\\", "");
System.out.println(teste3);
}
}
Your output is this:
a\b\c\d\e
abcde
abcde
See here working on ideone.
Oh, and remember that strings are immutable, so if you do this:
String pesquisa = "\uma\string\cheia\de\barras\invertidas\";
f = new File(path,prefix + "_" + dataArq + "_" + pesquisa.replaceAll("\\", "") + ".txt");
System.out.println(pesquisa);
The original string full of backslashes is what will appear in System.out.println
. On the other hand, if you do this:
String pesquisa = "\uma\string\cheia\de\barras\invertidas\";
String pesquisa2 = pesquisa.replaceAll("\\", ""); // Ou .replace("\", "");
f = new File(path,prefix + "_" + dataArq + "_" + pesquisa2 + ".txt");
System.out.println(pesquisa2); // É "pesquisa2"! Não é "pesquisa"!
There you will print the result without the backslashes.