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

Asked

Viewed 325 times

3

In a field I need to replace two words with any other two.

Example: Mr(a) v1, the value of your invoice is real v2. As it should be:
Mr(a) Joao, the value of your invoice is 10,00 real.

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 in this way, I can change only one word of the sentence, in this case v1.

Is there any other method where I can change two words in one sentence?

Thank you.

  • Why not call the replaceAll 2 times ?

  • Felipe, I tried, but it’s a mistake.

  • can post the most recent and complete code ? ( if this is the most recent note that var1 is being modified 3 times )

  • I’m sorry, fixed. And this is the latest code

1 answer

3


From what I read you want to replace:

Mr(a) V1, the value of your invoice is V2 real.

But in the code you’re giving replace in v1 (minuscule).

    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 go calling the function replaceAll passing the default and the value that will replace. :)

  • 1

    Bola Laerte Show. It worked, perfect. Many thanks to you who helped me.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.