Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Replacing all occurrences of characters in a string with one new character?

843785Oct 24 2008 — edited Oct 24 2008
Hi there,

I'm looking for a way that I could replace all occurrences of a set of specific characters with just one new character for each occurrence.

For example if I have a string which is "word1...word2.......word3....word4............word5" how would I be able to replace this with just one character such as ":" for each set of "." so that it would essentially appear like this "word1:word2:word3:word4:word5"

If I just use replace(".", ":") I am left with "word1:::word2:::::::word4::::word5::::::::::::"

So therefore I'm guessing this would require the use of replaceAll() maybe? but I'm not really very familiar with how regular expressions work and how this would be accomplished using them.

Any help would be greatly appreciated :) Thanks.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Nov 21 2008
Added on Oct 24 2008
5 comments
632 views