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!

How to replace multiple substrings inside a string?

807599Mar 29 2007 — edited Mar 29 2007
I was wondering, if you want to find a certain substring inside of a string and replace it with a new substring you can use this command (if initiated correctly):

String sentenceString =
JOptionPane.showInputDialog("Please enter a line of text with either "hello, hola or bonjour in it");
String hello = "hello";
String goodbye = "goodbye";
String hola = "hola";
String adios= "adios";
String bonjour = "bonjour";
String au revoir = "au revoir";

modified = sentenceString.replaceAll(hello, goodbye);
modified = sentenceString.replaceAll(hola, adios);
modified = sentenceString.replaceAll(bonjour, au revoir);



i figure i need some kind of loop to make this work, because using this method only the last one (bonjour) gets replace when used...the other two go unnoticed. How do i loop that?

....or am i way off on this one?
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Apr 26 2007
Added on Mar 29 2007
2 comments
259 views