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",".");
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.
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\\$", "&");
public String getPriceString() {
String priceString = "45.0";
String[] priceStringArray = priceString.split(".");
return priceStringArray.length + "";
}
Why does this give me a 0, zero? Shouldn't this be 2?
The argument to split() is a regular expression, and dot has a special meaning in regular expressions (it matches any character).
Try priceString.split("[.]");
You need to escape . like that
String[] priceStringArray = priceString.split("\\.");
split takes regular expression as a parameter and . means any character.
http://docs.oracle.com/javase/6/docs/api/java/util/regex/Pattern.html#sum
escape . with backslash like \\.. . is a regex metacharacter for anything. you will have to escape it with \\. in order to make it treat as a normal character
String priceString = "45.0";
String[] priceStringArray = priceString.split("\\.");
String.split takes a regular expression pattern. You're passing in . which means you want to split on any character.
You could use "\\." as the pattern to split on - but personally I'd use Guava instead:
private static final Splitter DOT_SPLITTER = Splitter.on('.');
...
(If you're not already using Guava, you'll find loads of goodies in there.)
You need to escape . as \\. because . has special meaning in regex.
String priceString = "45.0";
String[] priceStringArray = priceString.split("\\.");
return priceStringArray.length + "";
Use String[] priceStringArray = priceString.split("\\.");
You will have to use escape sequence.
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("[^]+", "");