Replace double quotes(") [duplicate] - java

This question already has answers here:
replace String with another in java
(7 answers)
Closed 9 years ago.
Here my String is looking like :-
sTest = AAAAA"1111
I want to replace double quote to a back ward slash and a double quotes(\")
I need the String Like
sTest = AAAAA\"1111

String escaped = "AAAAA\"1111".replace("\"", "\\\"");
API doc for String.replace
(Note that the replaceAll version handles regular expressions and is an overkill for this particular situation.)

string.replace("\"", "\\\"")
You want to replace " with \". Since both " and \ have a specific meaning you must escape them correctly by adding a preceding \ before each one.
So " --> \" and \" --> \\\".
And since you want the compiler to understand that this is a String, you need to wrap each string with double-quotes So " --> \" and "\"" --> "\\\"".

Although the other answers are correct for the single situation given, for more complicated situations, you may wish to use StringEscapeUtils.escapeJava(String) from Apache Commons Lang.
String escaped = StringEscapeUtils.escapeJava(string);

System.out.println("AAAAA\"1111".replaceAll("\"", "\\\\\""));

Related

why can't System.out.println print "C:\Users\Public"? [duplicate]

This question already has answers here:
How can I make Java print quotes, like "Hello"?
(11 answers)
What is the backslash character (\\)?
(6 answers)
Closed 2 years ago.
basically i want the output to be Path = "C:\Users\Public" and it seems to me that System.out.println("Path = "C:\Users\Public""); should work, but it doesn't so the question is why can't java just print the phrase as a combination of characters?
btw. this is my second time "programing" so please in simple terms if possible.
You should escape the special character such as " and \
System.out.println("Path = \"C:\\Users\\Public\"");
When you use backslash (\), java assumes that you are going to use an escape character. If you want to print that line you should probably use the method below and it should work. And you are using a string inside another one so you should use different types of inverted commas for both to tell the compiler that yeah there is a string inside another one. Otherwise the compiler will assume the middle inverted comma to be the closing one.
You can escape each and every escape character. If you notice I have used a backslash before inner inverted commas (" ") and every other backslash (\). Using a backslash before any escape character escapes it. So this should work.
System.out.println("Path = \"C:\\Users\\Public\"");
I hope now you can print your required output.

Replacing Regular expression matches in Java [duplicate]

This question already has answers here:
My regex is matching too much. How do I make it stop? [duplicate]
(5 answers)
Closed 4 years ago.
I want to replace &sp; in the string below with Z.
Input text : ABCD&sp;EF&p;GHIJ&bsp;KL
Output text : ABCDZEFZGHIZKL
Can anyone tell me how to replace the every instance of &\D+; using java regular expression?
I am using /(&\D+;)?/ but it doesn't work.
Use String#replaceAll.
You also should use the ? modificator to +:
String str = "ABCD&sp;EF&p;GHIJ&bsp;KL";
String regex = "&\\D+?;";
System.out.println (str.replaceAll(regex,"Z"));
This should work
Match the initial &, then all characters that are not the tailing ;, then that tailing ; like so: &[^;]+; If not matching numbers (as suggested by your example with \D) is a requirement, add the numbers to the negated character set: [^;0-9] To make it replace all occurrences, add the global flag g. The site regexr.com is a handy tool to create regexes.
Edit: Sorry, I initially read your question wrong.

String.split won't let me split with periods [duplicate]

This question already has answers here:
How can I use "." as the delimiter with String.split() in java [duplicate]
(8 answers)
Closed 9 years ago.
I am trying to do a String.split on a website address using the "." so that I can find the domain name of the website.
However, when I do this:
String href = "www.google.com";
String split[] = href.split(".");
int splitLength = split.length;
It tells me that the splitLength variable is 0. Why is this, and how can I make this work?
Try using this to split the string:
href.split("\\.");
Explanation: split splits on a regex, not on a regular substring. In regexes, . is the metacharacter for 'match any character', which we don't want. So we have to escape it using a backslash \. But \ is also a metacharacter for escaping in Java strings, so we need to escape it twice.
Split uses a regex so do:
String split[] = href.split("\\.");

How to add " " quotes around printed String? [duplicate]

This question already has answers here:
How can I make Java print quotes, like "Hello"?
(11 answers)
Closed 9 years ago.
I want to print inverted quotes in java. But how to print it?
for(int i=0;i<hello.length;i++) {
String s=hello[i].toLowerCase().trim();
System.out.println(""+s+"");
}
expected OP: "hi".....
Because double quotes delimit String values, naturally you must escape them to code a literal double quote, however you can do it without escaping like this:
System.out.println('"' + s + '"');
Here, the double quote characters (") have been coded as char values. I find this style easier and cleaner to read than the "clumsy" backslashing approach. However, this approach may only be used when a single character constant is being appended, because a 'char' is (of course) exactly one character.
As quotes are used in the Java source code to represent a string, you need to escape them to create a string that contains a quote
System.out.println("\""+s+"\"");
You must escape the quotes: \"
Assuming that by "Inverted" quotes you meant "Left" and "Right" specific quotation marks, you could do it like this:
System.out.println('\u201C'+s+'\u201D'); // Prints: “s”
System.out.println('"'+s+'"'); // Prints: "s"
If you are really looking for inverted quotes, use this:
System.out.println('\u201C' + s + '\u201D');
It'll output “hi”, not "hi".
You need to have a font installed, though, that supports this, otherwise you might get a box or something instead. Most Windows fonts do.

Replace all with a string having regex wild chars [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
java String.replaceAll without regex
I have a string and I need to replace some parts of it.
The replacement text contains regex wild chars though. Example:
String target = "Something * to do in ('AAA', 'BBB')";
String replacement = "Hello";
String originalText = "ABCDEFHGIJKLMN" + target + "ABCDEFHGIJKLMN";
System.out.println(originalText.replaceAll(target, replacement));
I get:
ABCDEFHGIJKLMNSomething * to do in ('AAA', 'BBB')ABCDEFHGIJKLMN
Why doesn't the replacement occur?
Because *, ( and ) are all meta-characters in regular expressions. Hence all of them need to be escaped. It looks like Java has a convenient method for this:
java.util.regex.Pattern.quote(target)
However, the better option might be, to just not use the regex-using replaceAll function but simply replace. Then you do not need to escape anything.
String.replaceAll() takes a regular expression and so it's trying to expand these metacharacters.
One approach is to escape these chars (e.g. \*).
Another would be to do the replacement yourself by using String.indexOf() and finding the start of the contained string. indexOf() doesn't take a regexp but rather a normal string.

Categories