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?
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());