How to convert String that contains String quotes even using replaceAll?

Asked

Viewed 859 times

4

I have a String as in the example below

String a = "Meu pai é um Grande "baio" de fada";

I want to make it a string like this

String a = "Meu pai é um Grande \"baio\" de fada";

how do I do this using replaceAll?

1 answer

6


replaceAll in quotes

It would be something like:

String a = "Meu pai é um Grande "baio" de fada";
a = a.replaceAll("\"", "\\\"");

In this case will exchange any quotes found, regardless if it is "test or "test".

replaceAll between quotes

But your question seems to me that it is more with Regex, the use I believe that is this:

String a = "Meu pai é um Grande "baio" de fada";
a = a.replaceAll("\"([^\"]+)\"", "\"$1\"");

In this case you will find everyone inside the quotation marks, for example "?asd?asd/A/sd;;lasd"

replaceAll between quotes containing text

In case it might be something with texts:

String a = "Meu pai é um Grande "baio" de fada";
a = a.replaceAll("\"([a-zA-Z0-9\s]+)\"", "\"$1\"");

In this case you will find everyone inside the quotation marks, for example "a e 0 9"

  • 1

    Thank you, you answered my question

Browser other questions tagged

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