I want to replace "
from a string with ^
.
String str = "hello \"there";
System.out.println(str);
String str1 = str.replaceAll("\"", "^");
System.out.println(str1);
String str2= str1.replaceAll("^", "\"");
System.out.println(str2);
and the output is :
hello "there
hello ^there
"hello ^there
why I am getting extra "
in start of string and ^
in between string
I am expecting:
hello "there
the replaceAll()
method consume a regex for the 1st argument.
the ^
in String str2= str1.replaceAll("^", "\"");
will match the starting position within the string.
So if you want the ^
char, write \^
Hope this code can help:
String str2= str1.replaceAll("\\^", "\"");
Try using replace
which doesnt use regex
String str2 = str1.replace("^", "\"");
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