String.replaceAll()

2 respostas
marcelohonsa

Galera, estou com um problema…
Ta dando pau nesse método…

Alguem sabe o que tem de errado???

private String getRespostaFormatada(String string) {

string = string.replaceAll("{EMAIL}", this.cliente.getEmail());
    string = string.replaceAll("{USUARIO}", this.usuario.getNome());
    string = string.replaceAll("{SOLICITAÇÃO}", this.solicitacao.getNumero_solicitacao());
    return string;
}

2 Respostas

_fs

Os colchetes são caracteres reservador em expressões regulares, então deve escapá-los:

String s = "as{EMAIL}df".replaceAll( "\\{EMAIL\\}", "TESTE" );
System.out.println( s );
fenrir

Você não diz exatamente qual é o erro, mas olhando assim, por cima, eu diria que você deveria substituir as chaves por parênteses.

string = string.replaceAll("(EMAIL)", this.cliente.getEmail());
string = string.replaceAll("(USUARIO)", this.usuario.getNome());
string = string.replaceAll("(SOLICITAÇÃO)", this.solicitacao.getNumero_solicitacao());
Criado 5 de outubro de 2006
Ultima resposta 5 de out. de 2006
Respostas 2
Participantes 3