How do i replace " character in Java? - java

I have
String a = "data=\"0\"1\"1\"1\"1\"0\"0\"0\"0\"0\"0\"1\"1\"1\"1\"0\"0\"0\"0\"0\"1\"1\"1\"1\"1\\\\";
How can i replace
" to \"
and \ to \\
?
String result = a.replace("\"", "\\\"");
OR
String result = a.replace(""", "\"");

String result = a.replace("\\","\\\\").replace("\"", "\\\"");
This would first replace all \ with \\ and then all " with \" if that is what you want.
Note that doing it the other way round would result in " being replaced with \\" in the end, since first it get replaced with \" and then the \ would be replaced with \\ resulting in \\".
Additional note: your data string is not well-formed and should not compile: it ends in \" which is not a valid string literal delimiter (the literal ends in \\\\\" which would be the string data \\") - change that to an even number of slashes or add another " to the end in order to fix that.

The former. The latter is not well-formed Java code.

Since "String result = a.replace(""", "\"");" does not compile, does that answer your question?

you a string error,Less a quote
String a = "data=\"0\"1\"1\"1\"1\"0\"0\"0\"0\"0\"0\"1\"1\"1\"1\"0\"0\"0\"0\"0\"1\"1\"1\"1\"1\\\"";
System.out.println(a.replace("\"", "\\\""));

Related

Using backslash (\) inside a String variable in java [duplicate]

This question already has answers here:
What is the backslash character (\\)?
(6 answers)
Closed last year.
I am not able to get the message value in the desired format.
String url = "sample"
String message ="/test{\"url\":"' + url + '\"}
The desired value of message is "/test{\"url\":\"sample\"}"
Any idea on this?
Try with this:
String url = "sample";
String message ="/test{\\\"url\\\":\""+url+"\\\"}";
Or you can use String.format:
String url = "sample";
String message = String.format("/test{\\\"url\\\":\"%s\\\"}",url);
Try the following syntax:
String url = "sample";
String message ="/test{\\\"url\\\":\"" + url + "\\\"}";
Please note that back-slash \ and double-quote " are specialized character and hence they need to be escaped using back-slash \.
Hence, \\ is used for \ and \" is used for " in String literal.
Output:
/test{\"url\":"sample\"}
After several tried, I found the solution:
StringBuilder sb=new StringBuilder();
sb.append("\"/test{");
sb.append("\"\\");
sb.append("\"");
sb.append("url\\\"");
sb.append(":");
sb.append("\\\"");
sb.append(url);
sb.append("\"\\}\"");
System.out.println(sb.toString());

How do I find and replace all escaped quotes and regular quotes with different values?

I'm looking to replace all occurrences of an escaped quote (\") with (\\\") in the string, then replacing all remaining unescaped quotes (") with escaped quotes (\"). Here's what I tried so far:
row = row.replaceAll("\\\\(?>\")", "\\\\\"");
row = row.replaceAll("((?<!\\\\)\")", "\"");
Example Input:
"This is a test with \" and "'s where \" is replaced with triple \'s before "
Example Output: \"This is a test with \\\" and \"'s where \\\" is replaced with triple \'s before \"
\\(?>\")" works on https://www.freeformatter.com/java-regex-tester.html#ad-output in replaceAll doesn't find escaped quotes.
Any help on this is appreciated.
It looks like you need to have four \'s to find a . I used a lookback and forward to find \". Credit to java, regular expression, need to escape backslash in regex.
"\\\\(?>\")" will find \".
"(?<!\\\\)\"" will find "'s without \ before it.
So the solution I found to do both is:
Pattern escapePattern = Pattern.compile("\\\\(?>\")");
Pattern quotePattern = Pattern.compile("(?<!\\\\)\"");
for(String row : rows.split("\n")) {
Matcher escapeMatcher = escapePattern.matcher(row.trim());
String escapedString = escapeMatcher.replaceAll("\\\\\\\\\\\\\"");
Matcher quoteMatcher = quotePattern.matcher(escapedString);
queryRows.add(quoteMatcher.replaceAll("\\\\\""));
}
Just replace single backslash with triple backslash, then replace quotes with backslash-quote:
row = row.replaceAll("\\\\(?!')", "\\\\\\\\\\\\").replace("\"", "\\\"");
first replace single ( \ ) with ( \\ ) and then replace ( " ) with ( \" )
row = row.replace("\\", "\\\\").replace("\"", "\\\"");

Regex for string between quotes and replace it

Can you guys help me??
I have a string here :
a$20=A.createVar("/LIST/S_UNB/C_S001/D_0001/*var", a$1, this);
Now I want to replace the string in "" with the value that appears after last '/'.
here I want result to be
a$20=A.createVar("*var", a$1, this);
I am trying to use as minimal objects as possible and my regex looks like this
\"([^\"]*)\"
Is this correct?
Assuming the quotes aren't part of the expression, use
[^/]+$
$ signifies the end of the string, which will make it return only the value after the last '/'.
You can use this code:
String s = "a$20=A.createVar(\"/LIST/S_UNB/C_S001/D_0001/*var\", a$1, this);";
// extract text between ""
String sub = s.replaceAll("^[^\"]*\"([^\"]*)\".*$", "$1");
// find last index of /
int i = sub.lastIndexOf('/');
// replace content between "" by token after last /
String repl = s.replaceFirst("\"[^\"]*\"", '"' + sub.substring(i+1) + '"');
//=> a$20=A.createVar("*var", a$1, this);

String.replace() is not working

What I have is a string array that I am creating from a .csv file I am reading. I then want to parse the values I'm going to use for the ' character and replace it with a \' because I am outputting this to a javascript file.
Here's the code I'm using for that:
while ((thisLine = myInput.readLine()) != null) {
String[] line = thisLine.split("\t");
if(line[4].indexOf("'") > -1){
System.out.println(line[4]);
line[4] = line[4].replace("'", "\'");
System.out.println(line[4]);
}
brand.add(line[4]);
}
However this is not working. I am getting the same string back after I do the replace.
Is this because of some issue with the string array?
I appreciate any assistance in this matter.
Try like this:
line[4] = line[4].replace("'", "\\'");
The backslash must be "escaped".
In case of line[4] = line[4].replace("'", "\'"); the part \' is converted to just '
You're falling foul of the fact that "'" is the same as "\'". They're the same string (a single character, just an apostrophe) - the escaping is there to allow a character literal of '\''.
You want:
line[4] = line[4].replace("'", "\\'");
So now you're escaping the backslash, instead of the apostrophe. So you're replacing apostrophe with backslash-then-apostrophe, which is what you wanted.
See JLS section 3.10.6 for details of escaping in character and string literals.
you should add back slash \ something like this
line[4] = line[4].replace("'", "\\'");
because one left slash \ is escape character
Your issue looks like it is an escape issue. Try \\ to replace a single back slash.

how to check if a space is followed by a certain character?

I am really confused on this regex things. I have tried to understand it, went no where.
Basically, i am trying to replace all spaces followed by every character but a space to be replaced with "PM".
" sd"
" sd"
however
" sd"
" sd"
This will replace the space and the following character with "PM":
String s = "123 axy cq23 dasd"; //your string
String newString = s.replaceAll(" [^ ]","PM");
Since I'm not sure if you want to replace only the space or the space and the following character, too, here is a slightly modified version that replaces only the space:
String s = "123 axy cq23 dasd"; //your string
String newString = s.replaceAll(" ([^ ])", "PM$1")
You need to use non-capturing pattern:
String res = oldString.replaceAll(" (?:[^ ])", "PM");

Categories

Resources