String replace function - java

I have following string
String str = "replace :) :) with some other string";
And I want to replace first occurance of :) with some other string
And I used str.replaceFirst(":)","hi");
it gives following exception
"Unmatched closing ')'"
I tried using replace function but it replaced all occurance of :).

The replaceFirst method takes a regular expression as its first parameter. Since ) is a special character in regular expressions, you must quote it. Try:
str.replaceFirst(":\\)", "hi");
The double backslashes are needed because the double-quoted string also uses backslash as a quote character.

The first argument to replaceFirst() is a regular expression, not just a character sequence. In regular expressions, the parantheses have special significance. You should escape the paranthesis like this:
str = str.replaceFirst(":\\)", "hi");

Apache Jakarta Commons are often the solution for this class of problems. In this case, I would have a look at commons-lang, espacially StringUtils.replaceOnce().

Related

Regex not working for replacing special characters

I want to replace special characters with nothing. So i tried
this.name.replace("[^a-zA-Z]+", "").trim()
I wnat the below word to be 000 Vektor
OOO "Vektor"
The documentation of replace says:
Replaces each substring of this string that matches the literal target sequence with the specified literal replacement sequence.
It won't take regular expressions.
The documentation of replaceAll says:
Replaces each substring of this string that matches the given regular expression with the given replacement.
So you may use:
this.name.replaceAll("[^a-zA-Z]+", "").trim();
You may also use replaceFirst with regular expressions, though not here.
Also, in a comment you say that you have tried it. I suspect that it is because you just use :
this.name.replaceAll("[^a-zA-Z]+", "").trim();
But java Strings are immutable, and don't change by themselves.
Hence you should use:
this.name = this.name.replaceAll("[^a-zA-Z]+", "").trim();
String.replace takes a literal first argument. replaceAll uses a regex
name = name.replaceAll("[^a-zA-Z ]+", "");

Why split method does not support $,* etc delimiter to split string

import java.util.StringTokenizer;
class MySplit
{
public static void main(String S[])
{
String settings = "12312$12121";
StringTokenizer splitedArray = new StringTokenizer(settings,"$");
String splitedArray1[] = settings.split("$");
System.out.println(splitedArray1[0]);
while(splitedArray.hasMoreElements())
System.out.println(splitedArray.nextToken().toString());
}
}
In above example if i am splitting string using $, then it is not working fine and if i am splitting with other symbol then it is working fine.
Why it is, if it support only regex expression then why it is working fine for :, ,, ; etc symbols.
$ has a special meaning in regex, and since String#split takes a regex as an argument, the $ is not interpreted as the string "$", but as the special meta character $. One sexy solution is:
settings.split(Pattern.quote("$"))
Pattern#quote:
Returns a literal pattern String for the specified String.
... The other solution would be escaping $, by adding \\:
settings.split("\\$")
Important note: It's extremely important to check that you actually got element(s) in the resulted array.
When you do splitedArray1[0], you could get ArrayIndexOutOfBoundsException if there's no $ symbol. I would add:
if (splitedArray1.length == 0) {
// return or do whatever you want
// except accessing the array
}
If you take a look at the Java docs you could see that the split method take a regex as parameter, so you have to write a regular expression not a simple character.
In regex $ has a specific meaning, so you have to escape it this way:
settings.split("\\$");
The problem is that the split(String str) method expects str to be a valid regular expression. The characters you have mentioned are special characters in regular expression syntax and thus perform a special operation.
To make the regular expression engine take them literally, you would need to escape them like so:
.split("\\$")
Thus given this:
String str = "This is 1st string.$This is the second string";
for(String string : str.split("\\$"))
System.out.println(string);
You end up with this:
This is 1st string.
This is the second strin
Dollar symbol $ is a special character in Java regex. You have to escape it so as to get it working like this:
settings.split("\\$");
From the String.split docs:
Splits this string around matches of the given regular expression.
This method works as if by invoking the two-argument split method with
the given expression and a limit argument of zero. Trailing empty
strings are therefore not included in the resulting array.
On a side note:
Have a look at the Pattern class which will give you an idea as to which all characters you need to escape.
Because $ is a special character used in Regular Expressions which indicate the beginning of an expression.
You should escape it using the escape sequence \$ and in case of Java it should be \$
Hope that helps.
Cheers

Why does the following regex not work for Java?

I want to delete a word and all its trailing whitespace.
Here is my regex:
item.getName().replace(word + "(\\s*)?", "");
I tested this statement by running:
item.getName().replace(word, "");
This executes successfully, albeit with extra whitespaces. So the error must be due to "(\\s*)?" part. Is it because I did not escape the slash correctly? Or does Java not recognize something in that regex?
replace uses a String literal as its first argument. Use replaceAll instead
String.replace method does not take regular expressions. I believe you'd have to use replaceAll in orer to use regular expression. Also, regular expressions are a general grammar that expresses a certain pattern of String rather than a particular instances that contain certain words. You can't mix a word with a regular expression such way.

Why does String.replaceAll() need so many escapes for " character?

If I have string a"b"c", but I want to get a\"b\"c\", I would naturally write
String t = "a\"b\"c\"";
t = t.replaceAll("\"", "\\\"");
However, that results in the same string, a"b"c". The correct way is
t.replaceAll("\"", "\\\\\"");
Why?
replaceAll uses regular expressions for both the pattern and the replacement - both of which require backslashes to be escaped. So the regex replacement pattern you want for the second argument is:
\\"
Now because both \ and " in Java string literals also need escaping, that means each of those characters needs an extra backslash. Add the quotes, and you've got:
"\\\\\""
which is what you've got in your source.
It's simpler if you just use String.replace which doesn't use regular expressions. That way you're only trying to provide this string (not string literal) as the second argument:
\"
After escaping and turning into a string literal, that becomes:
"\\\""
which still isn't great, but it's at least better.
An alternative is to use replaceAll but with Matcher.quoteReplacement:
t = t.replaceAll("\"", Matcher.quoteReplacement("\\\""));
Personally I'd just use replace() though. You don't want regular expression replacements, after all.

how to replace a string in Java

I have a question about using replaceAll() function.
if a string has parentheses as a pair, replace it with "",
while(S.contains("()"))
{
S = S.replaceAll("\\(\\)", "");
}
but why in replaceAll("\\(\\)", "");need to use \\(\\)?
Because as noted by the javadocs, the argument is a regular expression.
Parenthesis in a regular expression are used for grouping. If you're going to match parenthesis as part of a regular expression they must be escaped.
It's because replaceAll expects a regex and ( and ) have a special meaning in a regex expressions and need to be escaped.
An alternative is to use replace, which counter-intuitively does the same thing as replaceAll but takes a string as an input instead of a regex:
S = S.replace("()", "");
First, your code can be replaced with:
S = S.replace("()", "");
without the while loop.
Second, the first argument to .replaceAll() is a regular expression, and parens are special tokens in regular expressions (they are grouping operators).
And also, .replaceAll() replaces all occurrences, so you didn't even need the while loop here. Starting with Java 6 you could also have written:
S = S.replaceAll("\\Q()\\E", "");
It is let as an exercise to the reader as to what \Q and \E are: http://regularexpressions.info gives the answer ;)
S = S.replaceAll("\(\)", "") = the argument is a regular expression.
Because the method's first argument is a regex expression, and () are special characters in regex, so you need to escape them.
Because parentheses are special characters in regexps, so you need to escape them. To get a literal \ in a string in Java you need to escape it like so : \\.
So () => \(\) => \\(\\)

Categories