String#replaceAll() method not replacing for $ (dollar) [duplicate] - java

This question already has answers here:
Java regular expressions and dollar sign
(5 answers)
Closed 6 years ago.
I've tried built-in method String#replaceAll() to replace all "$" from my String content. But it's not working.
String ss = "HELLO_$_JAVA";
System.out.println(ss.indexOf("$"));
System.out.println(ss);
ss = ss.replaceAll("$", "");
System.out.println(ss);// 'HELLO__JAVA' is expected
OUTPUT:
6
HELLO_$_JAVA
HELLO_$_JAVA
Expected output:
6
HELLO_$_JAVA
HELLO__JAVA
EDIT:
Although Java regular expressions and dollar sign covers the answer, but still my question may be helpful for someone who is facing same problem when using String#replaceAll().
And
Difference between String replace() and replaceAll() also may be helpful.
Two possible solution of that question is
ss = ss.replace("$", "");
OR
ss = ss.replaceAll("\\$", "");

The first parameter of the replaceAll method takes a regular expression, not a literal string, and $ has a special meaning in regular expressions.
You need to escape the $ by putting a backslash in front of it; and the backslash needs to be double because it has a special meaning in Java string literals.
ss = ss.replaceAll("\\$", "");

String.replaceAll is for regular expressions. '$' is a special character in regular expressions.
If you are not trying to use regular expressions, use String.replace, NOT String.replaceAll.

May be not the best way but a work around,
String parts[] = ss.split("\\$");
Then concat each of them
String output = "";
for(String each:parts){
output=output+each;
}
Then you have your replaced string in output

Related

How to replace "\" with "/" in java? [duplicate]

This question already has answers here:
Java replaceAll: Cannot replace string with backslash
(3 answers)
What are all the escape characters?
(5 answers)
Closed 11 months ago.
I have a string like s = "abc\def" and I want to replace "" with "/" and makes the string like "abc/def/".
I tried replaceAll("\\","/") but the compiler is giving error for string
error: illegal escape character
String s="abc\def";
The issue is that Java uses \ as the string escape character, but also as the regex escape character, and replaceAll performs a regex search. So you need to doubly escape the backslash (once to make it a literal \ in the string, and once to make it a literal character in the regular expression):
result = str.replaceAll("\\\\", "/");
Alternatively, you don’t actually need regular expressions here, so the following works as well:
result = str.replace("\\", "/");
Or, indeed, using the single-char replacement version:
result = str.replace('\\', '/');

Replace the $ symbol in String [duplicate]

This question already has answers here:
Java regular expressions and dollar sign
(5 answers)
Closed 3 years ago.
How to replace all "$$$" present in a String?
I tried
story.replaceAll("$$$","\n")
This displays a warning: Anchor $ in unexpected position and the code fails to work. The code takes the "$" symbol as an anchor for a regular expression. I just need to replace that symbol.
Is there any way to do this?
"$" is a special character for regular expressions.
Try the following:
System.out.println(story.replaceAll("\\$\\$\\$", "\n"));
We are escaping the "$" character with a '\' in the above code.
There are several ways you can do this. It depends on what you want to do, and how elegant your solution is:
String replacement = "\n"; // The replacement string
// The first way:
story.replaceAll("[$]{3}", replacement);
// Second way:
story.replaceAll("\\${3}", replacement);
// Third way:
story.replaceAll("\\$\\$\\$", replacement);
You can replace any special characters (Regular Expression-wise) by escaping that character with a backslash. Since Java-literals use the backslash as escaping-character too, you need to escape the backslash itself.
story.replaceAll("\\${3}", something);
By using {3}behind the $, you say, that it should be found exactly three times. Looks a bit more elegant than "\\$\\$\\$".
something is thus your replacement, for example "" or \n, depending on what you want.
this will surely work..
story.replaceAll("\\$\\$\\$","\n")
YOu can do this for any special character.

Java String Split Behavior - Split on Dollar Symbol [duplicate]

This question already has answers here:
Why can't I split a string with the dollar sign?
(6 answers)
Closed 7 years ago.
I am trying the following code (running java version 1.7 in Eclipse Luna IDE on Ubuntu Linux 12.04):
String str = "abc$xyz";
String[] split_ = str.split("$");
System.out.println(split_.length);
I am always getting a split of length 1. If I try to print split_[0], I am always getting the entire string. Can anyone suggest what might be the cause?
This is because split expects a regular expression. Since "$" is the end-of-line marker in a regular expression, this only splits on the end of the String.
You should use
String str = "abc$xyz";
String[] split_ = str.split("\\$");
System.out.println(split_.length);
instead.
This escapes the "$", so that it's treated as a literal character instead (and uses two slashes to escape the backslash as part of the string literal).
The $ character is a metacharacter meaning "end of line", not a literal dollar sign.
Escape the $ character with two backslashes, one to escape the $ in the regular expression, one for a Java escape for a backslash.
String[] split_ = str.split("\\$");
.split() uses regex that is why...
Try this:
String str = "abc$xyz";
String[] split_ = str.split("\\$");
System.out.println(split_.length);

replaceAll Dangling metacharacter [duplicate]

This question already has answers here:
replace String with another in java
(7 answers)
Closed 7 years ago.
I have a string, say 1++++----2 and I want to replace ++++---- with a certain string, say string.
The I use the java function replaceAll, but it keep warning Dangling metacharacter every time I use it:
mystring.replaceAll("++++----", "string");
Escape the +, only the first or all doesn't matter here.
String str = "1+++---2";
str = str.replaceAll("\\+\\+\\+---", "");
System.out.println(str);
Output:
12
Or use replaceAllas it's meant to be used:
str = str.replaceAll("[+-]", "");
replaceAll's first argument takes a regualr expression and + have special meaning in regualr expression. Escape + to make it work properly .
mystring.replaceAll("\\+\\+\\+\\+----", "string");
You can use following regex :
mystring.replaceAll("\\++-+", "string")
Since + is a regex character you need to escape it.so here in "\\++-+" the first part \\+ will match the character + literally and the second + will match 1 or more combination of character + and the rest is -+ which will match 1 or more -.
When you replace a fixed string, you need a replace function:
String mystring = "1++++----2";
System.out.println(mystring.replace("++++----", "string"));
See demo
Otherwise, you need a regex with replaceAll.
System.out.println(mystring.replaceAll("[+]+-+", "string"));
Note that you do not need to escape the + inside a regex character class, which is convenient in Java.

| not recognized in java string.split() method [duplicate]

This question already has answers here:
Splitting a Java String by the pipe symbol using split("|")
(7 answers)
Closed 8 years ago.
I am having problems with the java string.split method.
I have a string word like so, which equals- freshness|originality. I then split this string like so:
String words[] = word.split("|");
If I then output words[1], like so:
t1.setText(words[1]);
It gives me the value f. I have worked out that this is the f in the word freshness.
How can I split the string properly so that words[1] is actually originality? Thanks for the help!
You should escape it:
String words[] = word.split("\\|");
Check this explanation in similar question here: Why does String.split need pipe delimiter to be escaped?
String object's split() method has a regular expression as a parameter. That means an unescaped | is not interpreted as a character but as OR and means "empty string OR empty string".
You need to escape the pipe because java recognizes it as a Regular Expression OR Operator.
line.split("\\|")
"|" gets is parsed as "empty string or empty string," which isn't what you are trying to find.
For the record
... ? . + ^ : - $ *
are all Regex Operators and need to be escaped.
You need to escape the character. Use "\\|".
More information on regex escaped characters here.
String test ="freshness|originality";
String[] splits = test.split("\\|");
String part1 = splits[0]; // freshness
String part2 = splits[1]; // originality

Categories

Resources