How to replace multiple substrings inside a string?
807599Mar 29 2007 — edited Mar 29 2007I 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?