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());
Related
This question already has answers here:
How do I split a string in Java?
(39 answers)
Closed 2 years ago.
I have a string like this.
PER*IP**TE**1234567890*EM*sampleEmail#Email.com
How can I parse the string into multiple lines like this in Java?
PER
IP
TE
//Empty String
EM
1234567890
sampleEmail#Email.com
You could use a regex replacement:
String input = "PER*IP**TE*1234567890*EM*sampleEmail#Email.com";
String output = input.replaceAll("\\*", "\n");
System.out.println(output);
This prints:
PER
IP
TE
1234567890
EM
sampleEmail#Email.com
You can use String#split. Since * is a regular expression metacharacter, you need to escape it with a backslash or use Pattern#quote.
Arrays.stream("PER*IP**TE**1234567890*EM*sampleEmail#Email.com".split(Pattern.quote("*")))
.forEach(System.out::println);
String newstring = string.replace("*", "\n");
System.out.println(newstring);
now if you don't want that the empty line show up, use this:
String string = "PER*IP**TE**1234567890*EM*sampleEmail#Email.com"
String newstring = string.replaceAll("\\*+","*").replace("*", "\n");
System.out.println(newstring);
This question already has an answer here:
regex: How to escape backslashes and special characters?
(1 answer)
Closed 8 years ago.
I was trying to escape a Json string as an input via Scanner and print to console,
i was not able to escape " \ " by replacing it with " \\ ",
I'm getting PatternSyntaxException
Here is my code
Scanner s = new Scanner(System.in);
String str = s.next();
String s3 = "";
if (str.contains("\\")) {
s3 = str.replaceAll("\\", "\\\\");
System.out.println(s3);
}
Here is my input to scanner
{"name":"nokia"}\
Help me please !
If you are using regex you have to use 4 backslashes \\\\ to parse the backslash as a literal.
So use s3 = str.replaceAll("\\\\", someOtherString);
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
This question already has answers here:
How to escape apostrophe or quotes on a JSP (used by JavaScript)
(9 answers)
Closed 9 years ago.
JSP:
<% final String data = "some test with ' single quotes"; %>
<script>
var str = '<%= data %>';
<script>
The result is (JavaScript):
var str = 'some test with ' single quotes';
Uncaught SyntaxError: Unexpected identifier
How do I replace this single quote with \' to avoid a JavaScript error?
Use escapeEcmaScript method from Apache Commons Lang package:
Escapes any values it finds into their EcmaScript String form. Deals
correctly with quotes and control-chars (tab, backslash, cr, ff, etc.).
So a tab becomes the characters '\\' and 't'.
The only difference between Java strings and EcmaScript strings is
that in EcmaScript, a single quote and forward-slash (/) are escaped.
Example:
input string: He didn't say, "Stop!"
output string: He didn\'t say, \"Stop!\"
Remember you also need to encode the double quotes, new lines, tabs and many other things. One way to do it is using org.apache.commons.lang.StringEscapeUtils
public class JavaScriptEscapeTest {
public static void main(String[] args) throws Exception {
String str = FileUtils.readFileToString(new File("input.txt"));
String results = StringEscapeUtils.escapeEcmaScript(str);
System.out.println(results);
}
}
input.txt
Here is some "Text" that
I'd like to be "escaped" for JavaScript.
I'll try a couple special characters here: \ "
output
Here is some \"Text\" that\r\nI\'d like to be \"escaped\" for JavaScript.\r\nI\'ll try a couple special characters here: \ \"
Are you looking to simply double-escape your string so you actually have a \ followed by a '?
<% final String data = "some name with \\' single quote"; %>
From your JavaScript code, you can use the string replace functionality:
var str2 = str1.replace("'","\\'");
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("\"", "\\\""));