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
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
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
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
You must escape special characters in the toReplace string, so that the regex interpret them right. You could try this:
toReplace = new SomeClass().someMethod().r eplaceAll( "\\(", "\\(");
toReplace = new SomeClass().someMethod().r
hoomanv supplied answer is the correct one, ignore my replace
ASKER
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 "\\(" ?
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 "\\(" ?
ASKER
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 );
String s = "FIELD ( .......)";
String toReplace = "\\Q"+(new SomeClass().someMethod())+
String replaceWith = "F (";
s = s.replaceAll(toReplace , replaceWith );
> I'll have to use replaceAll to first replace "(" with "\\(" ?
both for toReplace and replaceWith
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/do cs/api/jav a/util/reg ex/Pattern .html
java.sun.com/j2se/1.5.0/do
without the need to escape
"FIELD ( .......)".replace("(", "F (");
results in "FIELD F ( .......)"
"FIELD ( .......)".replace("(", "F (");
results in "FIELD F ( .......)"
ASKER
>> 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.
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?
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?
ASKER
>> 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
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.
ASKER
>>Which Java version are You using? Maybe there are some incompatibilities.
J2SE 1.4
J2SE 1.4
use replace instead of reaplaceAll
ASKER
>> use replace instead of reaplaceAll
It gives an error
replace(char,char) in java.lang.String cannot be applied to (java.lang.String,java.lan g.String)
It gives an error
replace(char,char) in java.lang.String cannot be applied to (java.lang.String,java.lan
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(toRep lace, replaceWith);
toReplace = toReplace.replaceAll("\\("
String replaceWith = "OR( FIELD (DATE >= 10-10-2005 ), FIELD (DATE2 >= 10-10-2005 ))";
replaceWith = replaceWith.replaceAll("\\
replaceOn = replaceOn.replaceAll(toRep
aah replace(String, String) has been added since 1.5
Sorry - typo
>>replaceAll("\\(", "\\\\");
should be
replaceAll("\\(", "\\\\(");
>>replaceAll("\\(", "\\\\");
should be
replaceAll("\\(", "\\\\(");
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
closing parenthesis should be escaped too
toReplace = toReplace.replaceAll("\\)" , "\\\\)");
toReplace = toReplace.replaceAll("\\)"
>>closing parenthesis should be escaped too
True
True
ASKER
>> This worked for me again:
Yes, it works. I was making a mistaking
Thanks a lot everyone...
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 ).replaceA ll(Matcher .quoteRepl acement(re placeWith) );
System.out.println(result) ;
import java.util.regex.*;
String result = Pattern.compile(toReplace,
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.
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.
toReplace = "Field \\(";