×
Namespaces

Variants
Actions
(Difference between revisions)

Normalize string whitespaces with J2ME

From Nokia Developer Wiki
Jump to: navigation, search
hamishwillee (Talk | contribs)
m (Hamishwillee - Bot update - Add ArticleMetadata)
lpvalente (Talk | contribs)
m (Lpvalente -)
 
(5 intermediate revisions by 2 users not shown)
Line 1: Line 1:
{{ArticleMetaData <!-- v1.2 -->
+
[[Category:General Programming on Java ME]][[Category:Code Snippet]]
 +
{{ArticleMetaData <!-- v1.3 -->
 
|sourcecode= <!-- Link to example source code e.g. [[Media:The Code Example ZIP.zip]] -->
 
|sourcecode= <!-- Link to example source code e.g. [[Media:The Code Example ZIP.zip]] -->
 
|installfile= <!-- Link to installation file (e.g. [[Media:The Installation File.sis]]) -->
 
|installfile= <!-- Link to installation file (e.g. [[Media:The Installation File.sis]]) -->
 
|devices= <!-- Devices tested against - e.g. ''devices=Nokia 6131 NFC, Nokia C7-00'') -->
 
|devices= <!-- Devices tested against - e.g. ''devices=Nokia 6131 NFC, Nokia C7-00'') -->
 
|sdk= <!-- SDK(s) built and tested against (e.g. [http://linktosdkdownload/ Qt SDK 1.1.4]) -->
 
|sdk= <!-- SDK(s) built and tested against (e.g. [http://linktosdkdownload/ Qt SDK 1.1.4]) -->
|platform= <!-- Compatible platforms - e.g. Symbian^1 and later, Qt 4.6 and later -->
 
|devicecompatability= <!-- Compatible devices e.g.: All* (must have internal GPS) -->
 
 
|dependencies= <!-- Any other/external dependencies e.g.: Google Maps Api v1.0 -->
 
|dependencies= <!-- Any other/external dependencies e.g.: Google Maps Api v1.0 -->
 
|signing= <!-- Signing requirements - empty or one of: Self-Signed, DevCert, Manufacturer -->
 
|signing= <!-- Signing requirements - empty or one of: Self-Signed, DevCert, Manufacturer -->
|capabilities= <!-- Capabilities required by the article/code example (e.g. Location, NetworkServices. -->
+
|capabilities= <!-- Required capabilities for code (e.g. ID_CAP_LOCATION, ID_CAP_NETWORKING) -->
|keywords= <!-- APIs, classes and methods (e.g. QSystemScreenSaver, QList, CBase -->
+
 
|language= <!-- Language category code for non-English topics - e.g. Lang-Chinese -->
 
|language= <!-- Language category code for non-English topics - e.g. Lang-Chinese -->
 
|translated-by= <!-- [[User:XXXX]] -->
 
|translated-by= <!-- [[User:XXXX]] -->
Line 21: Line 19:
 
|author= [[User:Jappit]]
 
|author= [[User:Jappit]]
 
}}
 
}}
[[Category:Java ME]][[Category:Code Snippet]]
+
{{Abstract|This article demonstrates how to normalize white spaces in strings.}}
 +
 
 
A simple method to normalize String multiple whitespaces (replacing them with a single whitespace). For example, to normalize the String:<br/>
 
A simple method to normalize String multiple whitespaces (replacing them with a single whitespace). For example, to normalize the String:<br/>
 
''"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Normalize&nbsp;&nbsp;&nbsp;&nbsp;all&nbsp;&nbsp;whitespaces&nbsp;&nbsp;"''
 
''"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Normalize&nbsp;&nbsp;&nbsp;&nbsp;all&nbsp;&nbsp;whitespaces&nbsp;&nbsp;"''

Latest revision as of 16:52, 27 December 2013

Article Metadata
Article
Created: jappit (06 May 2008)
Last edited: lpvalente (27 Dec 2013)

This article demonstrates how to normalize white spaces in strings.

A simple method to normalize String multiple whitespaces (replacing them with a single whitespace). For example, to normalize the String:
"     Normalize    all  whitespaces  "
to:
" 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) == ' ')
{
currIndex++;
}
 
prevIndex = currIndex;
}
res.append(s.substring(prevIndex));
 
return res.toString();
}
This page was last modified on 27 December 2013, at 16:52.
83 page views in the last 30 days.
×