This week's giveaways are in the MongoDB and Jobs Discussion forums.
We're giving away four copies of Mongo DB Applied Patterns and 4 resume reviews from Five Year Itch and have the authors/reps on-line!
See this thread and this one for details.
The moose likes Java in General and the fly likes string.replaceFirst(String,String) not working Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of Mongo DB Applied Patterns this week in the MongoDB forum
or a resume review from Five Year Itch in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "string.replaceFirst(String,String) not working" Watch "string.replaceFirst(String,String) not working" New topic
Author

string.replaceFirst(String,String) not working

Satyajit Bhadange
Ranch Hand

Joined: May 13, 2010
Posts: 103
hi,

i was using string.replaceFirst(String,String) but it didnt work in my company where i am working.

what might be the problem


Thanks
user101
Problems And Solutions - Algorithms
Carl Trusiak
Sheriff

Joined: Jun 13, 2000
Posts: 3340
Without you posting the exact String you want to replace, I can only assume the somehow fall into a Regular expression and it's inappropriate. I suggest you look at java.util.regex.Pattern to determine the correct pattern to use to find what needs replaced.


I Hope This Helps
Carl Trusiak, SCJP2, SCWCD
James Sabre
Ranch Hand

Joined: Sep 07, 2004
Posts: 781

Satyajit Bhadange wrote:hi,

i was using string.replaceFirst(String,String) but it didnt work in my company where i am working.

what might be the problem


Since I have no trouble with the method I assume you have a bug in your code. Note - Strings are immutable so the original string is not modified and the method returns a new String. If this is not the problem then I bet you have a fault in your regular expression.

Of course, it would be easier for the forum members to diagnose the problem if you posted your code.


Retired horse trader.
 Note: double-underline links may be advertisements automatically added by this site and are probably not endorsed by me.
Satyajit Bhadange
Ranch Hand

Joined: May 13, 2010
Posts: 103
i was calling replace.First() method because i wanted to replace each occurrence of String to be replace and recorded

method call was as below

sQuery = sQuery .replaceFirst("$"+svalue,"SOME NAME");

when i talk to my team members about this they told me that it is machine problem because they face same problem before..

is it really a machine problem or or it just a wrong coding..??
James Sabre
Ranch Hand

Joined: Sep 07, 2004
Posts: 781

Satyajit Bhadange wrote:i was calling replace.First() method because i wanted to replace each occurrence of String to be replace and recorded

method call was as below

sQuery = sQuery .replaceFirst("$"+svalue,"SOME NAME");

when i talk to my team members about this they told me that it is machine problem because they face same problem before..

is it really a machine problem or or it just a wrong coding..??


The $ character is a meta character and has special meaning in regular expressions (it means the end of input). You need to escape it.


sQuery = sQuery .replaceFirst("\\$"+svalue,"SOME NAME");

You should really escape the whole of the regex and the replacement in this case just in case svalue or the replacement contain other meta characters. i.e.

sQuery = sQuery .replaceFirst(Pattern.quote("$"+svalue),Matcher.quoteReplacement("SOME NAME"));
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 16680
    
  19

Satyajit Bhadange wrote:
when i talk to my team members about this they told me that it is machine problem because they face same problem before..

is it really a machine problem or or it just a wrong coding..??


So... your colleagues couldn't get it to work. And blamed the hardware?


It's a regex issue -- as someone will likely beat me to the explanation...

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Satyajit Bhadange
Ranch Hand

Joined: May 13, 2010
Posts: 103
Thank you so much..

satisfy with your answer...but is still need to try it..
Satyajit Bhadange
Ranch Hand

Joined: May 13, 2010
Posts: 103
Thank you...

Thank you...

it worked.....
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Please take this opportunity to laugh and point at your co-workers.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: string.replaceFirst(String,String) not working
 
Similar Threads
rtrim function in jdk 1.4
Delimiting Strings
Delimiting Strings
string search & replace
Can you simulate String.replace(...) & String.contains(...)