Namespaces
Variants
Actions
Revision as of 08:53, 8 December 2011 by hamishwillee (Talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Como normalizar uma String com espaços em branco, usando Java ME

Jump to: navigation, search
Dados do artigo

Artigo
Tradução:
Última alteração feita por hamishwillee em 08 Dec 2011

Um simples método para normalizar strings com múltiplos espaços em branco( substituindo-os por espaço simples). Por exemplo para normalizar o string :
"     Normalize    todos  os espaços em branco  "
to:
" Normalize todos os espaços em branco ".

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) == ' ')
{
currIndex++;
}
 
prevIndex = currIndex;
}
res.append(s.substring(prevIndex));
 
return res.toString();
}
121 page views in the last 30 days.
Nokia Developer aims to help you create apps and publish them so you can connect with users around the world.

京ICP备05048969号  © Copyright Nokia 2013 All rights reserved