Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Implement a string splitter based on a given string delimiter

by Retired ‎02-16-2010 03:11 PM - edited ‎09-16-2010 03:02 PM (4,142 Views)

Summary

 

This article applies to the following:

  • BlackBerry® smartphones based on Java® technology

Details

 

Many software development application programming interfaces (APIs), frameworks and libraries contain a useful string method that can split a given string into a string array based on a given string delimiter. This can be used in many different scenarios, but mainly for the purpose of parsing large strings into more manageable string arrays that can be processed in a loop.

 

The BlackBerry APIs do not contain such a method, so splitting a string must be implemented manually.

 

The following example shows one such implementation of a string split method:

 

 

//Identifies the substrings in a given string that are delimited
//by one or more characters specified in an array, and then
//places the substrings into a String array.
public static String[] split(String strString, String strDelimiter) {
String[] strArray;
int iOccurrences = 0;
int iIndexOfInnerString = 0;
int iIndexOfDelimiter = 0;
int iCounter = 0;

//Check for null input strings.
if (strString == null) {
throw new IllegalArgumentException("Input string cannot be null.");
}
//Check for null or empty delimiter strings.
if (strDelimiter.length() <= 0 || strDelimiter == null) {
throw new IllegalArgumentException("Delimeter cannot be null or empty.");
}

//strString must be in this format: (without {} )
//"{str[0]}{delimiter}str[1]}{delimiter} ...
// {str[n-1]}{delimiter}{str[n]}{delimiter}"

//If strString begins with delimiter then remove it in order
//to comply with the desired format.

if (strString.startsWith(strDelimiter)) {
strString = strString.substring(strDelimiter.length());
}

//If strString does not end with the delimiter then add it
//to the string in order to comply with the desired format.
if (!strString.endsWith(strDelimiter)) {
strString += strDelimiter;
}

//Count occurrences of the delimiter in the string.
//Occurrences should be the same amount of inner strings.
while((iIndexOfDelimiter = strString.indexOf(strDelimiter,
iIndexOfInnerString)) != -1) {
iOccurrences += 1;
iIndexOfInnerString = iIndexOfDelimiter +
strDelimiter.length();
}

//Declare the array with the correct size.
strArray = new String[iOccurrences];

//Reset the indices.
iIndexOfInnerString = 0;
iIndexOfDelimiter = 0;

//Walk across the string again and this time add the
//strings to the array.
while((iIndexOfDelimiter = strString.indexOf(strDelimiter,
iIndexOfInnerString)) != -1) {

//Add string to array.
strArray[iCounter] = strString.substring(iIndexOfInnerString,iIndexOfDelimiter);

//Increment the index to the next character after
//the next delimiter.
iIndexOfInnerString = iIndexOfDelimiter +
strDelimiter.length();

//Inc the counter.
iCounter += 1;
}

return strArray;
}


 

Example usage

 

String strTestString = "||string1||string2||string3||string5";
String[] arrString = split(strTestString,"||");

for (int i=0; i< arrString.length; i++) {
System.out.println("array element " + i + ": " + arrString[i]);
}

  

Output

 

array element 0: string1
array element 1: string2
array element 2: string3
array element 3: string5

String strTestString2 = "The quick brown fox jumps over the lazy dog.";
String[] arrString2 = split(strTestString2," ");

for (int i=0; i< arrString2.length; i++) {
System.out.println("array element " + i + ": " + arrString2[i]);
}

  

Output

 

array element 0: The 
array element 1: quick
array element 2: brown
array element 3: fox
array element 4: jumps
array element 5: over
array element 6: the
array element 7: lazy
array element 8: dog.

 

Additional Methods

 

The BlackBerry® Java® Development Environment (BlackBerry JDE) API comes with a similar method that can split a string into a string array. The StringUtilities.stringToWords methods are a combination of methods that extract words from a given string.

 

A word is defined as a contiguous block of uppercase and lowercase characters, with the adjacent characters at either end (if they exist) not being alphanumeric. The only difference between this method and the method shown previously is that you cannot specify a custom delimiter; therefore, you can only split words and keywords.

 

The following example shows the usage of the stringToWords method:

 

 

String str = "The quick brown fox jumps over the lazy dog.";
String[] arrStr = StringUtilities.stringToKeywords(str);
for (int i=0; i< arrStr.length; i++) {
System.out.println("array element " + i + ": " + arrStr[i]);
}

 

 Output

 

array element 0: The 
array element 1: quick
array element 2: brown
array element 3: fox
array element 4: jumps
array element 5: over
array element 6: the
array element 7: lazy
array element 8: dog.

 

 

 

Contributors
Users Online
Currently online: 32 members 2,448 guests
Please welcome our newest community members: