You are here

J2ME Tip: Replacing strings

Amr M. Kamel's blog - Fri, 14/09/2007 - 3:37pm
This is a method you can use to replace substring inside a string, I know its very easy in JSE but in JME there is no regex, no replaceAll() method.. I posted it hoping that it might be handy for you ;)


String replaceAll(String text, String searchString, String replacementString){
StringBuffer sBuffer = new StringBuffer();
int pos = 0;
while((pos = text.indexOf(searchString)) != -1){
sBuffer.append(text.substring(0, pos) + replacementString);
text = text.substring(pos + searchString.length());
}
sBuffer.append(text);
return sBuffer.toString();
}