List of all Iphone and Blackberry Development codes in a one click Iphone,objective C,xcode,blackberry Development,iOS Development
Showing posts with label String Split. Show all posts
Showing posts with label String Split. Show all posts

Tuesday, February 5, 2013

String splitting in Blackberry

The following code will help you to split a string
String s="A,B,C,D,";
String[] n=split(s,",");
 for (int i = 0; i < n.length; i++)
{
 LabelField l=new LabelField();
 l.setText(n[i]);
add(l);
}

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.");
        }
        if (strString.startsWith(strDelimiter)) {
            strString = strString.substring(strDelimiter.length());
        }
        if (!strString.endsWith(strDelimiter)) {
            strString += strDelimiter;
        }
        while((iIndexOfDelimiter = strString.indexOf(strDelimiter,
               iIndexOfInnerString)) != -1) {
            iOccurrences += 1;
            iIndexOfInnerString = iIndexOfDelimiter +
                strDelimiter.length();
        }

        strArray = new String[iOccurrences];
        iIndexOfInnerString = 0;
        iIndexOfDelimiter = 0;
        while((iIndexOfDelimiter = strString.indexOf(strDelimiter,
               iIndexOfInnerString)) != -1) {

            strArray[iCounter] = strString.substring(iIndexOfInnerString,iIndexOfDelimiter);
            iIndexOfInnerString = iIndexOfDelimiter +
            strDelimiter.length();
            iCounter += 1;
        }

        return strArray;
    }
    

The answer will be - A B C D