Yatin
Yatin

Reputation: 3120

How to split a string in Blackberry

I want to know how to split a String in Blackberry The str.split() function seems to be unavailable

Upvotes: 0

Views: 1522

Answers (4)

Nilanchala
Nilanchala

Reputation: 5941

Check this out, the simple one

     public static String[] split(String str, char c) {
        int index = str.indexOf(c);
        int count = 0;
        for (int i = 0; i < str.length(); i++) {
            if (str.charAt(i) == c)
                count++;
        }

        String[] words = new String[++count];
        int counter = 0;
        while (index >= 0) {
            words[counter] = str.substring(0, index);
            str = str.substring(index + 1,str.length()).trim();
            counter++;
            index = str.indexOf(c);
        }

        words[counter] = str;           
        return words;
    }

Upvotes: 2

Arun Kumar Munusamy
Arun Kumar Munusamy

Reputation: 871

@YAK: Try this one..

public void split(String Word,Char delimiter)
String[] arr = new String[5];
    String text = "pen, pencil,book,123,note";
    text=text+delimiter;
    int n = 0;
    for (int i = 0; i < text.length(); i++)
    {
            int s = text.indexOf(delimiter);
            add(new RichTextField(Integer.toString(s)));
            if(s==0)
            {
             arr[n]="null";
             if(text.length()>1)
             {
             text = text.substring(1,text.length());
             i = 0;
                n++;
             }
            }
            else
            {
            arr[n] = text.substring(0, s);
            s = s + 1;
            text = text.substring(s,text.length());
           // add(new RichTextField("txt"+text));
            i = 0;
            n++;
            }       

    }
    for (int i = 0; i < arr.length; i++)
    {
        if (arr[i] != null)
        {
            add(new RichTextField("value: "+arr[i]));

        }

    }  

Upvotes: 0

Govindarao Kondala
Govindarao Kondala

Reputation: 2862

This is preferred link

http://supportforums.blackberry.com/t5/Java-Development/String-Manipulation-split-replace-replaceAll/ta-p/620038

if above link not working then go throgh following

public String[] split(String strString, String strDelimiter)
    {
        int iOccurrences = 0;
        int iIndexOfInnerString = 0;
        int iIndexOfDelimiter = 0;
        int iCounter = 0;

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

        // 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.
        String[] 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;
    } 

Upvotes: 0

BBdev
BBdev

Reputation: 4942

Yes you are right, the split() function is not provided in Blackberry api. I have to use that so i have made it by this way. May be it will be helpful for you too .

public static String[] split(String original, String separator) {

    Vector nodes = new Vector();               
    int index = original.indexOf(separator);      
    while (index >= 0) {                   
        nodes.addElement(original.substring(0, index));           
        original = original.substring(index + separator.length());          
        index = original.indexOf(separator);       
    }       
    nodes.addElement(original);              
    String[] result = new String[nodes.size()];       
    if (nodes.size() > 0) {           
        for (int loop = 0; loop < nodes.size(); loop++) {               
            result[loop] = (String) nodes.elementAt(loop);               
            System.out.println("Value inside result is ........ "+ result[loop]);           
        }       
    }      
    return result;   
}

Upvotes: 1

Related Questions