I need some help from you guys. I have a string name = "john"
But I want to save this String name as "john"
, including ""
(quotations)
String name = ""john""; String name1 = "[john]"
Can some one help me with this.
If you need to use the double quote inside the string, you can use the backslash character. Notice how the backslash in the second line is used to escape the double quote characters. And the single quote can be used without a backslash.
Use the String. replaceAll() method to remove all double quotes from a string, e.g. str. replaceAll('"', '') . The replace() method will return a new string with all double quotes removed.
Within a character string, to represent a single quotation mark or apostrophe, use two single quotation marks. (In other words, a single quotation mark is the escape character for a single quotation mark.) A double quotation mark does not need an escape character.
String name = "\"john\"";
You have to escape the second pair of quotation marks using the \ character in front. It might be worth looking at this link, which explains in some detail.
Other scenario where you set variable:
String name2 = "\""+name+"\"";
Sequence in console:
> String name = "\"john\""; > name ""john"" > String name2 = "\""+name+"\""; > name2 """john"""
You need to escape the quotation marks:
String name = "\"john\"";
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With