This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Java in General and the fly likes How to replace first occarnce of Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "How to replace first occarnce of "." in  a String?" Watch "How to replace first occarnce of "." in  a String?" New topic
Author

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

Kumara Swamy
Ranch Hand

Joined: Nov 17, 2008
Posts: 34
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.
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2896
    
    1

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


[LEARNING bLOG] | [Freelance Web Designer] | [and "Rohan" is part of my surname]
Kumara Swamy
Ranch Hand

Joined: Nov 17, 2008
Posts: 34
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

Joined: Feb 19, 2008
Posts: 2896
    
    1

You are welcome..
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32708
    
    4
Sagar Rohankar, well done. A good example of how to help somebody to learn
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2896
    
    1

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
Sheriff

Joined: Oct 13, 2005
Posts: 32708
    
    4
Aw shucks . . . you've got me all embarrassed now.

You're welcome
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to replace first occarnce of "." in a String?
 
Similar Threads
Substituting strings
Recursive anagram problem
string question
String.replaceAll
function replace substring