Replace all " characters to \" Java - java

How replace all " characters with \". I have some String, like this:
{"ConfirmId":"0d8de6a6-15f1-4084-ba3a-4ebfe3151b38","Amount":1.00,"Fee":0.00,"FullAmount":1.0}
And I need something like this answer:
{\"ConfirmId\":\"0d8de6a6-15f1-4084-ba3a-4ebfe3151b38\",\"Amount\":1.00,\"Fee\":0.00,\"FullAmount\":1.0}
How can I do this?
This is example of my part of code:
String replace = Answer.get(1);
replace = "\"" + replace.replaceAll("\"","\\\"") + "\"";
System.out.println(replace);
But it doesn't work nice. Have somebody any idea?

Change your line to following
replace = replace.replaceAll("\"", "\\\\\"");
It will change "Confimed" to \"Confimed\"

Related

How can I encode " ' "

I'm working on Java with URL and I don't know how to encode the " ' " character.
I don't want to use URLEncoder since it replaces spaces with + and I need %20
private String encoder(String param) {
return param.replaceAll("\\s", "%20").replaceAll("\\'", "%27");
}
This is the code i'm trying but it doesn't work, do you have any ideas on how to replace this " ' " ? Or know another method better than it?
Thank you!!
You don't need to escape ' symbol, just enclose it in double quotes
param.replaceAll("\\s", "%20").replaceAll("'", "%27");
Another way is to use java.net.URLEncoder class to encode URL and replace + symbols to %20
java.net.URLEncoder.encode("URL with spaces'", "UTF-8").replace("+", "%20")

Digits are getting deleted when splitting a string

I have a string from which I need to remove all mentioned punctuations and spaces. My code looks as follows:
String s = "s[film] fever(normal) curse;";
String[] spart = s.split("[,/?:;\\[\\]\"{}()\\-_+*=|<>!`~##$%^&\\s+]");
System.out.println("spart[0]: " + spart[0]);
System.out.println("spart[1]: " + spart[1]);
System.out.println("spart[2]: " + spart[2]);
System.out.println("spart[3]: " + spart[3]);
I have a string from which I need to remove all mentioned punctuations and spaces. My code looks as follows:
String s = "s[film] fever(normal) curse;";
String[] spart = s.split("[,/?:;\\[\\]\"{}()\\-_+*=|<>!`~##$%^&\\s+]");
System.out.println("spart[0]: " + spart[0]);
System.out.println("spart[1]: " + spart[1]);
System.out.println("spart[2]: " + spart[2]);
System.out.println("spart[3]: " + spart[3]);
But, I am getting some elements which are blank. The output is:
spart[0]: s
spart[1]: film
spart[2]:
spart[3]: normal
- is a special character in PHP character classes. For instance, [a-z] matches all chars from a to z inclusive. Note that you've got )-_ in your regex.
- defines a range in regular expressions as used by String.split argument so that needs to be escaped
String[] part = line.toLowerCase().split("[,/?:;\"{}()\\-_+*=|<>!`~##$%^&]");
String[] spart = s.split("[,/?:;\\[\\]\"{}()\\-_+*=|<>!`~##$%^&\\s]+");

Escape space with backslash in java

I want to replace spaces from path string. I tried below but doesn't seems to be working :
String path = "/Users/TD/San Diego";
path=path.replaceAll(" ","\\ ");
System.out.println(path);
Goal is to convert
"/Users/TD/San Diego" to "/Users/TD/San\ Diego"
Any further space from string also needs to be replaced with "\ "
You could change
path = path.replaceAll(" ", "\\ ");
to escape the backslash
path = path.replaceAll(" ", "\\\\ ");
When I do that, I get (the requested)
/Users/TD/San\ Diego
Another option would be using String.replace like
path = path.replace(" ", "\\ ")
which outputs the same.
The suggested solution did not work for me (in Android Java).
So this is what I've came up with, after quite a few attempts:
path = path.replace(" ", (char) 92 + " ");

How do i replace " character in 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("\"", "\\\""));

new line in java

Java newbie here, I'm having trouble setting a new line in this code:
String FnameTextboxText = FnameTextbox.getText();
String LastnameTextboxText = LastnameTextbox.getText();
String CourseTextboxText = CourseTextbox.getText();
Summary.setText("Firstname:" + " " + FnameTextboxText + "\nLastname:" + " " + LastnameTextboxText + "\nCourse:" + " " + CourseTextboxText);
Also tried something like: "\n" + "Lastname" But its no good.
Do you have any idea on how to make new lines. So that it'll look like this;
Firstname: x
Lastname: y
Course: Z
Using netbeans 6.8. On windows.
I guess you need to use TextArea.
First, use TextArea
Second, test using \r or \n or \r\n
Sometimes, people use \n to make new line and sometimes, like me, use \r\n to make new line

Categories

Resources