Link to home
Start Free TrialLog in
Avatar of thomas908
thomas908

asked on

String replaceAll method

I have a string with lots of braces ")" and "(".
Eg

String s = "FIELD ( .......)";

String toReplace = "Field (" ;
I want some part to be replaced by another string.

String replaceWith = "F (";

When I try to use the replaceAll function, I get no result.

s = s.replaceAll(toReplace , replaceWith );

It looks like it is because of the "(" in the string. Can someone please help me resolve this?
thanks
Avatar of StillUnAware
StillUnAware
Flag of Lithuania image

use this:

toReplace = "Field \\(";
Avatar of thomas908
thomas908

ASKER

Thanks for the quick response.
I am getting that String by calling some predefined methods (which I can't change).
It's something like

toReplace = new SomeClass().someMethod();

This gives me a string. I want to change some values in that string. Please let me know what should i do.
thanks
SOLUTION
Avatar of hoomanv
hoomanv
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You must escape special characters in the toReplace string, so that the regex interpret them right. You could try this:

toReplace = new SomeClass().someMethod().replaceAll("\\(", "\\(");
hoomanv supplied answer is the correct one, ignore my replace
Thanks for replying.
But both these Strings I am getting by calling some methods.

So, I am getting these values in variables. What should I do in this case.
I'll have to use replaceAll to first replace "(" with "\\(" ?
Sorry, misunderstood the answer...
Forget that, here is a cleaner solution. Leave everything as is, and change only the toReplace like that:

String s = "FIELD ( .......)";
String toReplace = "\\Q"+(new SomeClass().someMethod())+"\\E"; // quote all the search string
String replaceWith = "F (";
s = s.replaceAll(toReplace , replaceWith );
> I'll have to use replaceAll to first replace "(" with "\\(" ?
both for toReplace and replaceWith

I suggest You read about the regex. It's all about the string matching and replacing, and the String.replaceAll(...) makes use of it:

java.sun.com/j2se/1.5.0/docs/api/java/util/regex/Pattern.html
without the need to escape

"FIELD ( .......)".replace("(", "F (");
results in "FIELD F ( .......)"
>> Forget that, here is a cleaner solution. Leave everything as is, and change only the toReplace like that:

Not working. Its not giving any results.
Can you give us a *concrete* example of the String that you want to find, together with one you want to replace by?
Odd, this works for me:

    String s = "FIELD ( .......)";
    String toReplace = "\\Q"+"FIELD ("+"\\E";
    String replaceWith = "F (";
    s = s.replaceAll(toReplace , replaceWith );
    System.out.println(s);

and I get the result: F ( .......)

Can You give some more examples of strings toReplace and replaceWith?
>> Can you give us a *concrete* example of the String that you want to find, together with one you want to replace by?

String s = "( AND (  FIELD (NUMBER1 IN-FULLTEXT 'tuty5' ) ,  OR( FIELD (DATE >= 10-10-2005 ), FIELD (DATE1 >= 10-10-2005 ),FIELD (DATE3 >= 10-10-2005 )) , FIELD(TYPE IN ( '1', '2' )),FIELD(D IN '000000000'))  )"; //comes from a method call

String toReplace = "FIELD (DATE >= 10-10-2005 )" ; //comes from a method call

String replaceWith = "OR( FIELD (DATE >= 10-10-2005 ), FIELD (DATE2 >= 10-10-2005 ))"; ////comes from a method call

String s needs to be modified and toReplace needs to be replaced with text from replaceWith.

thanks
In the worst case, it may happen that the toReplace string somewhere at the begining has a combination of characters "\\E". In that case the PatternSyntaxException should be thrown, which didn't happen.

Which Java version are You using? Maybe there are some incompatibilities.
>>Which Java version are You using? Maybe there are some incompatibilities.
J2SE 1.4
use replace instead of reaplaceAll
>> use replace instead of reaplaceAll
It gives an error
replace(char,char) in java.lang.String cannot be applied to (java.lang.String,java.lang.String)
String toReplace = "FIELD (DATE >= 10-10-2005 )" ; //comes from a method call
toReplace = toReplace.replaceAll("\\(", "\\\\");
String replaceWith = "OR( FIELD (DATE >= 10-10-2005 ), FIELD (DATE2 >= 10-10-2005 ))";
replaceWith = replaceWith.replaceAll("\\(", "\\\\");
replaceOn = replaceOn.replaceAll(toReplace, replaceWith);
aah replace(String, String) has been added since 1.5
Sorry - typo

>>replaceAll("\\(", "\\\\");

should be

replaceAll("\\(", "\\\\(");
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
closing parenthesis should be escaped too
toReplace = toReplace.replaceAll("\\)", "\\\\)");
>>closing parenthesis should be escaped too

True
>> This worked for me again:
Yes, it works. I was making a mistaking
Thanks a lot everyone...
Standard Way

import java.util.regex.*;

String result = Pattern.compile(toReplace, Pattern.LITERAL).matcher(s).replaceAll(Matcher.quoteReplacement(replaceWith));
            System.out.println(result);

my last comment is the best way though :)
Thanks for accepting, I have another solution, maybe a little bit faster:

    String s = "( AND (  FIELD (NUMBER1 IN-FULLTEXT 'tuty5' ) ,  OR( FIELD (DATE >= 10-10-2005 ), FIELD (DATE1 >= 10-10-2005 ),FIELD (DATE3 >= 10-10-2005 )) , FIELD(TYPE IN ( '1', '2' )),FIELD(D IN '000000000'))  )"; //comes from a method call
    String toReplace = "FIELD (DATE >= 10-10-2005 )"; //comes from a method call
    String replaceWith = "OR( FIELD (DATE >= 10-10-2005 ), FIELD (DATE2 >= 10-10-2005 ))"; ////comes from a method call    s = s.replaceAll(toReplace , replaceWith );
    int pos = 0;
    while((pos = s.indexOf(toReplace, pos)) != -1) {
      s = s.substring(0, pos) + replaceWith + s.substring(pos + toReplace.length());
      pos = pos + replaceWith.length();
    }
    System.out.println(s);

this does not use regex and although it may be optimized even more.