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.
Related
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("\"", "\\\"");
Guys I want to add \ before double quote " in a string..
for example :-
String s = "FB "Party" event";
Output should be:-
String Output = "FB \"Party\" event"
I tried a lot but no luck.
Thanks
Try this "FB \\\"Party\\\" event"
First two slashes is one slash and \" is double quote
Use a simple technique..
use escape character. Like for your case it would be something like
String input = "FB \\\"Party\\\" event";
System.out.println(input);
I have a string value which received from input field.
String searchingText = getText();
After i receive a string i search this string. But if string contains \ symbol my search is failed.
I know about special characters and try to replace :
searchingText = searchingText.replaceAll("\\","\\\\");
But it give me error and app was shutdown.
Exception in thread "main" java.util.regex.PatternSyntaxException: Unexpected internal error near index 1
After research i founded a regex and try to replace with matcher :
Map<String,String> sub = new HashMap<String,String>();
sub.put("\n", "\\\\n");
sub.put("\r", "\\\\r");
sub.put("\t", "\\\\t");
StringBuffer result = new StringBuffer();
Pattern regex = Pattern.compile("\\n|\\r|\\t");
Matcher matcher = regex.matcher(bodySearchText);
In the end i will want to get a string - searchingText = \\ instead of searchingText = \
Please any solutions.
You should do:
string = string.replaceAll("\\\\", "\\\\\\\\");
Note that in Java, \ is written as \\. So replaceAll will see \\ as \, which is not what you want.
Instead, you can use replace that accepts String and not a regex.
change this
searchingText = searchingText.replaceAll("\\","\\\\");
to
searchingText = searchingText.replaceAll("\\\\","\\\\\\");
the replaceAll() will take \\ as \ .
for more details read here
I am using java replaceAll() method to escape new line characters
String comment = "ddnfa \n \r \tdnfadsf ' \r t ";
comment = comment.replaceAll("(\\n|\\r|\\t)","\\\\$1");
System.out.println(comment);
But the above code is still inserting new line.
Is there a way to output the comment exactly the same (i.e. with \n and \r instead of inserting new line)?
UPDATE:
I ended up using:
comment = comment.replaceAll("\\n","\\\\n")
.replaceAll("\\r","\\\\r")
.replaceAll("\\t","\\\\t");
You'll have to go one-by-one, since the new-line character U+000A has nothing to do with the two-character escape sequence \n:
comment = comment.replaceAll("\n","\\\\n");
comment = comment.replaceAll("\r","\\\\r");
comment = comment.replaceAll("\t","\\\\t");
you will have to do it character by character:
comment = comment.replaceAll("\n","\\\\n");
comment = comment.replaceAll("\r","\\\\r");
comment = comment.replaceAll("\t","\\\\t");
another solution is to escape the String as a Java String using this function:
comment = org.apache.commons.lang.StringEscapeUtils.escapeJava(comment);
This will make the String look exactly like the String in the Java Code, but it will also show other escape sequences (like \\, \" etc).
But maybe thats exactly what you want
Hard way: using Matcher
String comment = "ddnfa \n \r \tdnfadsf ' \r t ";
Map<String,String> sub = new HashMap<String,String>();
sub.put("\n", "\\\\n");
sub.put("\r", "\\\\r");
sub.put("\t", "\\\\t");
StringBuffer result = new StringBuffer();
Pattern regex = Pattern.compile("\\n|\\r|\\t");
Matcher matcher = regex.matcher(comment);
while (matcher.find()) {
matcher.appendReplacement(result, sub.get(matcher.group()));
}
matcher.appendTail(result);
System.out.println(result.toString());
prints
ddnfa \n \r \tdnfadsf ' \r
Why you dont use Matcher.quoteReplacement(stringToBeReplaced);?
It is a \ problem, simplify like this :
comment = comment.replaceAll("(\n|\r|\t)", "");
output :
ddnfa dnfadsf ' t
Try this..
comment.replaceAll("(\n)|(\r)|(\t)", "\n");
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("\"", "\\\""));