Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries over the next few weeks. Thanks for all your past and future contributions.
Normalize string whitespaces with J2ME
m (Hamishwillee - Adding missing translation link)
|Line 36:||Line 36:|
Revision as of 04:23, 16 December 2011
A simple method to normalize String multiple whitespaces (replacing them with a single whitespace). For example, to normalize the String:
" Normalize all whitespaces "
" Normalize all whitespaces ".
String normalizeWhitespaces(String s)
StringBuffer res = new StringBuffer();
int prevIndex = 0;
int currIndex = -1;
int stringLength = s.length();
String searchString = " ";
while((currIndex = s.indexOf(searchString, currIndex + 1)) >= 0)
res.append(s.substring(prevIndex, currIndex + 1));
while(currIndex < stringLength && s.charAt(currIndex) == ' ')
prevIndex = currIndex;