50% OFF!!!

Monday, June 8, 2009

J2ME | String Replace method



I found a good string replace function/method.
J2ME do not contain string replace (only char replace),
so this method is very helpful!!!



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
sb.append(_text.substring(startPos,_text.length()));

return sb.toString();
}





found at http://forums.sun.com/thread.jspa?threadID=734604&tstart=5431

1 comment: