Use more than one variable in the same sentence (replace)

3

In a precise field, replace two words with another two words.

Example: Sr (a) v1, the value of your invoice is v2 real. How to get:
Mr Joao , your invoice amount is 10,00 .

So, I have the following method below.

String mensagemI = txtMensagem.getText();

        String var1 = txtVariavel1.getText();
        String var2 = txtVariavel2.getText();
        String var3 = txtVariavel3.getText();

        String mensagemF = mensagemI.replaceAll("v1", var1);

        System.out.println(mensagemF);

But this way, I can change only one word of the phrase, in this case v1.

Is there another method where I can change two words in a single sentence?

Thank you.

    
asked by anonymous 26.07.2016 / 13:27

1 answer

3

From what I read you want to replace:

  

Mr V1 , your invoice amount is V2 .

But in the code you're giving replace in v1 (lowercase).

    String mensagemI = "Sr(a) V1, o valor da sua fatura é de V2 reais.\nPague sua fatura em dia V1.";

    String var1 = "Joao";
    String var2 = "10,00";

    String mensagemF = mensagemI.replaceAll("V1", var1).replaceAll("V2", var2);

    System.out.println(mensagemF);

Just call the replaceAll function by passing the pattern and the value it will replace. :)

    
26.07.2016 / 13:41