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.

Revision as of 17:44, 7 September 2013 by lpvalente (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

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

From Wiki
Jump to: navigation, search
Article Metadata

Por valderind4
Última alteração feita por lpvalente em 07 Sep 2013

Este artigo demonstra como "normalizar" os espaços em branco dentro de uma string.

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  "
" 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) == ' ')
prevIndex = currIndex;
return res.toString();
This page was last modified on 7 September 2013, at 17:44.
40 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.


Thank you!

We appreciate your feedback.