• 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

How to replace first occarnce of "." in a String?

 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I used the replaceFirst() to solve the problem but replaceFirst() method behaviour is very strange when we are dealing with ".".

The scenarios i executed are as follows:

1)This scenario worked as expected.
Input :"Hello.world.app".replace(".", "@");
Output : Hello@world@app



2)Giving wild behaviour.
Input :"Hello.world.app".replaceFirst(".", "@")
Output : @ello.world.app


3)Giving wild behaviour.
Input :"Hello.world.app".replaceAll(".", "@")
Output : @@@@@@@@@@@@@@@


4)This scenario worked as expected.
Input :"Hello.world.app".replaceAll("p", "@")
Output : Hello.world.a@@

5)This scenario worked as expected.
Input :"Hello.world.app".replaceFirst("p", "@")
Output : Hello.world.a@p


Can any one tell me why the replaceFirst(), replaceAll() methods failed to replace the "." ?

Thanks,
Kumar.
 
Ranch Hand
Posts: 2908
1
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Kumara Swamy D:
Hi,
I used the replaceFirst() to solve the problem but replaceFirst() method behaviour is very strange when we are dealing with ".".

The scenarios i executed are as follows:



2)Giving wild behaviour.
Input :"Hello.world.app".replaceFirst(".", "@")
Output : @ello.world.app




Can you look at the Java docs for the above methods, its clearly mention that first parameter is taken as Regular Expression. If you are good with Regular expression, you don't face this problem OR Study Regular Expression in Java
 
Kumara Swamy
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sagar Rohankar,

Thanks for your quick and perfect reply.
The url helped me a lot to know about "regex".
The issue is resolved.

Thanks once again

Thanks,
Kumar.
 
Sagar Rohankar
Ranch Hand
Posts: 2908
1
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are welcome..
 
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sagar Rohankar, well done. A good example of how to help somebody to learn
 
Sagar Rohankar
Ranch Hand
Posts: 2908
1
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Campbell Ritchie:
Sagar Rohankar, well done. A good example of how to help somebody to learn



that's because, I learnt lots from your post and fellow Ranchers. Really a good site for beginners like me .. I highly appreciate the efforts taken by all Ranchers..

 
Campbell Ritchie
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Aw shucks . . . you've got me all embarrassed now.

You're welcome
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic