I have string variable strVar with value as ' "value1" ' and i want to replace all the double quotes in the value with ' \" '. So after replacement value would look like ' \"value1\" '
How to do this in java?
Kindly help me.
You are looking for
str = str.replace("\"", "\\\"")
DEMO
I would avoid using replaceAll since it uses regex syntax in description of what to replace and how to replace, which means that \ will have to be escaped in string "\\" but also in regex \\ (needs to be written as "\\\\" string) which means that we would need to use
str = str.replaceAll("\"", "\\\\\"");
or probably little cleaner:
str = str.replaceAll("\"", Matcher.quoteReplacement("\\\""))
With replace we have escaping mechanism added automatically.
actually it is:
strVar.replaceAll("\"", "\\\\\"");
For example take a string which has structure like this--->>>
String obj = "hello"How are"you";
And you want replace all double quote with blank value or in other word,if you want to trim all double quote.
Just do like this,
String new_obj= obj.replaceAll("\"", "");
Strings are formatted with double quotes. What you have is single quotes, used for chars. What you want is this:
String foo = " \"bar\" ";
This should give you what you want;
System.out.println("'\\\" value1 \\\"'");
To replace double quotes
str=change condition to"or"
str=str.replace("\"", "\\"");
After Replace:change condition to\"or\"
To replace single quotes
str=change condition to'or'
str=str.replace("\'", "\\'");
After Replace:change condition to\'or\'
Related
I have a string with \r\n, \r, \n or \" characters in it. How can I replace them faster?
What I already have is:
String s = "Kerner\\r\\n kyky\\r hihi\\n \\\"";
System.out.println(s.replace("\\r\\n", "\n").replace("\\r", "").replace("\\n", "").replace("\\", ""));
But my code does not look beautiful enough.
I found on the Internet something like:
replace("\\r\\n|\\r|\\n|\\", "")
I tried that, but it didn't work.
You can wrap it in a method, put /r/n, /n and /r in a list. iterate the list and replace all such characters and return the modified string.
public String replaceMultipleSubstrings(String original, List<String> mylist){
String tmp = original;
for(String str: mylist){
tmp = tmp.replace(str, "");
}
return tmp;
}
Test:
mylist.add("\\r");
mylist.add("\\r\\n");
mylist.add("\\n");
mylist.add("\\"); // add back slash
System.out.println("original:" + s);
String x = new Main().replaceMultipleSubstrings(s, mylist);
System.out.println("modified:" + x);
Output:
original:Kerner\r\n kyky\r hihi\n \"
modified:Kerner kyky hihi "
I don't know if your current replacement logic be correct, but it says now that either \n, \r, or \r\n gets replaced with empty string, and backslash also gets replaced with empty string. If so, then you can try the following regex replace all:
String s = "Kerner\\r\\n kyky\\r hihi\\n \\\"";
System.out.println(s.replaceAll("\\r|\\n|\\r\\n|\\\\", ""));
One problem I saw with your attempt is that you are calling replace(), not replaceAll(), so it would only do a single replacement and then stop.
String.replaceAll() can be used, in your question you tried to use String.replace() which does not interpret regular expressions, only plain replacement strings...
You also need to escape the \\ again, i.e. \\\\ instead of \\
String s = "Kerner\\r\\n kyky\\r hihi\\n \\\"";
System.out.println(s.replaceAll("\\\\r|\\\\n|\\\\\"", ""));
Output
Kerner kyky hihi
Note the differences between String.replaceAll() and String.replace()
String.replaceAll()
Replaces each substring of this string that matches the given regular
expression with the given replacement.
String.replace()
Replaces each substring of this string that matches the literal target
sequence with the specified literal replacement sequence.
Use a regular expression if you want to do all the replaces in one go.
http://www.javamex.com/tutorials/regular_expressions/search_replace.shtml
Iam trying to get the string october from the string ["october"]
i tried with replace method
tagNo = tagNo.replaceAll("\\[|\\]", ""); \\ it prints "october"
i cant replace quote with "".
tagNo = tagNo.replaceAll("\\[|\\]", "").replaceAll("\"", "\\\\\"");
not replacing quotes prints \"october\"
But i prefer to use a single code
using regex code how can i get the string with out quotes and square bracket
You need to slightly correct your regex. You haven't put any rule for removing double quote. Try using this,
tagNo = tagNo.replaceAll("(\\[|\"|\\])+", "");
tagNo = tagNo.replaceAll("[^a-zA-Z]+","");
tagNo = tagNo.replaceAll("\\[\"", "");
tagNo = tagNo.replaceAll("\"\\]", "");
Refer Java replace all square brackets in a string
Removing double quotes from a string in Java
After executing the below line the b contains the value "\%AMPAMP\$". I want it to be "&". Please help.
String b = a.replaceAll("\%AMPAMP\$", "&");
String is immutable. See the public String replaceAll(String regex,String replacement):
Returns:
The resulting String
You should do:
a = a.replaceAll("\%AMPAMP\$", "&");
Edit:
After you said that you did save it, you should now notice that replaceAll takes a regex and not a String. You should escape the special characters (Escaping a regex is done by \, but in Java \ is written as \\), or use String#quote:
a = a.replaceAll(Pattern.quote("\%AMPAMP\$"), "&");
You don't really need a regex here. Use String#replace(String search, String replace) method like this:
b = a.replace("%AMPAMP$", "&");
btw String#replaceAll method needs a regex where you need to use double backslash to escape $:
b = a.replaceAll("%AMPAMP\\$", "&");
String s ="SSR/DANGEROUS GOODS AS PER ATTACHED SHIPPERS
/DECLARATION 1 PACKAGE
NFY
/ACME CONSOLIDATORS"
How to strip the space between "PACKAGE" and "NFY" ?
Java's String.replaceAll in fact takes a regular expression. You could remove all newlines with:
s = s.replaceAll("\\n", "");
s = s.replaceAll("\\r", "");
But this will remove all newlines.
Note the double \'s: so that the string that is passed to the regular expression parser is \n.
You can also do this, which is smarter:
s = s.replaceAll("\\s{2,}", " ");
This would remove all sequences of 2 or more whitespaces, replacing them with a single space. Since newlines are also whitespaces, it should do the trick for you.
Try this code:
s = s.replaceAll( "PACKAGE\\s*NFY", "PACKAGE NFY" );
s = s.replaceAll("[\\n\\r]", "");
Have you tried a replace function? Something in the lines of:
youString.Replace("\r", "")
string = string.replace(/\s{2,}/g, ' ');
I have a String called persons.name
I want to replace the DOT . with /*/ i.e my output will be persons/*/name
I tried this code:
String a="\\*\\";
str=xpath.replaceAll("\\.", a);
I am getting StringIndexOutOfBoundsException.
How do I replace the dot?
You need two backslashes before the dot, one to escape the slash so it gets through, and the other to escape the dot so it becomes literal. Forward slashes and asterisk are treated literal.
str=xpath.replaceAll("\\.", "/*/"); //replaces a literal . with /*/
http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#replaceAll(java.lang.String,%20java.lang.String)
If you want to replace a simple string and you don't need the abilities of regular expressions, you can just use replace, not replaceAll.
replace replaces each matching substring but does not interpret its argument as a regular expression.
str = xpath.replace(".", "/*/");
Use Apache Commons Lang:
String a= "\\*\\";
str = StringUtils.replace(xpath, ".", a);
or with standalone JDK:
String a = "\\*\\"; // or: String a = "/*/";
String replacement = Matcher.quoteReplacement(a);
String searchString = Pattern.quote(".");
String str = xpath.replaceAll(searchString, replacement);
return sentence.replaceAll("\s",".");