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\\$", "&");
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
Consider the following code:
String str = "folder1;b";
String replacement = "C:\\myfolder";
System.out.println(str.replaceAll("b", replacement));
This printsC:myfolder.
How can I replace str with the replacement string as is? (Without the slashes being removed)
I've tried Pattern.quote(replacement) but that prints \QC:\Development\E
I have no control over replacement which comes from an external source and it is not known what its contents would be.
If you aren't using regular expressions, better to use String.replace():
String str = "folder1;b";
String replacement = "C:\\myfolder";
System.out.println(str.replace("b", replacement));
String.replace() does a literal replacement, it doesn't treat the arguments as regular expressions.
I will point out that you will run into trouble if your str is folder1;bash;b as both of the bs will be replaced.
String.replaceAll uses regex. You don't need that.
Try this:
String str = "b";
String replacement = "C:\\myfolder";
System.out.println(str.replace(str,replacement);
The second parameter of String.replaceAll expect a regex. In the regex world a \ has a special operator meaning. You have to escape it once for the jvm and once for regex.
String str = "b";
String replacement = "C:\\\\\\\\myfolder";
System.out.println(str.replaceAll(str, replacement));
Will print out
C:\\myfolder
With String.replace it does take the literal string and thus you only have to escape it once for the jvm
String str = "b";
String replacement = "C:\\\\myfolder";
System.out.println(str.replace(str, replacement));
Will print
C:\\myfolder
And lastly, if you have no idea how the incoming replacement String looks like you can escape it beforehand with
String str = "b";
String replacement = "C:\\myfolder"; // might be anything
String actualReplacement = replacement.replaceAll("\\\\", "\\\\\\\\");
System.out.println(str.replace(str, actualReplacement));
Will print
C:\\myfolder
Other than that you could use one of the apache.utils to achieve it, but this one here is without any third party library.
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\'
I want to split the following string "Good^Evening" i used split option it is not split the value. please help me.
This is what I've been trying:
String Val = "Good^Evening";
String[] valArray = Val.Split("^");
I'm assuming you did something like:
String[] parts = str.split("^");
That doesn't work because the argument to split is actually a regular expression, where ^ has a special meaning. Try this instead:
String[] parts = str.split("\\^");
The \\ is really equivalent to a single \ (the first \ is required as a Java escape sequence in string literals). It is then a special character in regular expressions which means "use the next character literally, don't interpret its special meaning".
The regex you should use is "\^" which you write as "\\^" as a Java String literal; i.e.
String[] parts = "Good^Evening".split("\\^");
The regex needs a '\' escape because the caret character ('^') is a meta-character in the regex language. The 2nd '\' escape is needed because '\' is an escape in a String literal.
try this
String str = "Good^Evening";
String newStr = str.replaceAll("[^]+", "");
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",".");