Class to Replace String in J2ME (JME)


There is no String.replaceAll in JME, so I search and found useful resource from

public static String replace(String _text, String _searchStr, String _replacementStr) {
// String buffer to store str
StringBuffer sb = new StringBuffer();

// Search for search
int searchStringPos = _text.indexOf(_searchStr);
int startPos = 0;
int searchStringLength = _searchStr.length();

// Iterate to add string
while (searchStringPos != -1) {
sb.append(_text.substring(startPos, searchStringPos)).append(_replacementStr);
startPos = searchStringPos + searchStringLength;
searchStringPos = _text.indexOf(_searchStr, startPos);

// Create string

return sb.toString();

Thank you, Jijo!

1 komentar:

  1. Nice snippet! Better than JME default String.replace() that supports only character replacement.


speak now or forever hold your peace

About Me