• Post Reply Bookmark Topic Watch Topic
  • New Topic

string.replaceFirst(String,String) not working  RSS feed

 
Satyajit Bhadange
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,

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

what might be the problem
 
Carl Trusiak
Sheriff
Posts: 3341
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
James Sabre
Ranch Hand
Posts: 781
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Satyajit Bhadange
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 781
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Satyajit Bhadange
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you so much..

satisfy with your answer...but is still need to try it..
 
Satyajit Bhadange
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you...

Thank you...

it worked.....
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please take this opportunity to laugh and point at your co-workers.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!