• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

string.replaceFirst(String,String) not working

 
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 3341
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 781
Netbeans IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Netbeans IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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"));
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you...

Thank you...

it worked.....
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please take this opportunity to laugh and point at your co-workers.
 
reply
    Bookmark Topic Watch Topic
  • New Topic