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

Reply
New Developer
Posts: 48
Registered: ‎12-11-2008
My Device: Not Specified

"How to split string"

HI guys,

 

How to split the long string based on the white space. For .net am using  ' mystring.Split(" ") '  using this property i can split the whole string.   we can get all words of the long string at one string array()................

 

 

In Blackberry have any inbulid method  to split

Developer
Posts: 3,951
Registered: ‎07-15-2008
My Device: Not Specified

Re: "How to split string"

Use StringUtilities...

 

StringUtilities su = null;
String[] abc = su.stringToWords("Xyz XYZ xyz xyZ");

 

Tnq.


Use Search. "Accept Solution" If the problem is resolved.
New Developer
Posts: 48
Registered: ‎12-11-2008
My Device: Not Specified

Re: "How to split string"

Hi,

 

Thaks for ur reply. i can get result correctly. but when am using special char (/,-.&...)  i cant able to split it correctly.

 

Example: mystring ="Praise th-e lo&rd"

 

here i need ,

 str(0)---- Praise

str(1)-----th-e

str(2)-----lo&rd

 

 

But i got,

 

 str(0)---- Praise

str(1)-----th

str(2)-----e

str(1)-----lo

str(2)-----rd

 

Let me know if u have any any idea.....

 

Expect u.....

 

 

 

Developer
Posts: 46
Registered: ‎07-12-2008
My Device: Not Specified

Re: "How to split string"

Here is a helper method I've written to do this:

 

public static final Vector split(final String data, final char splitChar) { Vector v = new Vector(); String working = data; int index = working.indexOf(splitChar); while (index != -1) { String tmp = ""; if (index > 0) { tmp = working.substring(0, index); } v.addElement(tmp); working = working.substring(index + 1); // Find the next index index = working.indexOf(splitChar); } // Add the rest of the working string v.addElement(working); return v; }

 

For small strings this method is okay.  Using this on large strings tends to cause memory problems.

Highlighted
New Developer
Posts: 6
Registered: ‎05-07-2009
My Device: Not Specified

Re: "How to split string"

public static final String[] splitString(final String data, final char splitChar, final boolean allowEmpty) { Vector v = new Vector(); int indexStart = 0; int indexEnd = data.indexOf(splitChar); if (indexEnd != -1) { while (indexEnd != -1) { String s = data.substring(indexStart, indexEnd); if (allowEmpty || s.length() > 0) { v.addElement(s); } indexStart = indexEnd + 1; indexEnd = data.indexOf(splitChar, indexStart); } if (indexStart != data.length()) { // Add the rest of the string String s = data.substring(indexStart); if (allowEmpty || s.length() > 0) { v.addElement(s); } } } else { if (allowEmpty || data.length() > 0) { v.addElement(data); } } String[] result = new String[v.size()]; v.copyInto(result); return result; }

 

Its pretty much the same as above, but it doesn't use any temporary variables, so I think it should be better on the memory use side of thing.  Instead it just cuts up the data input string as necessary.  I also needed the ability to skip any blank lines (I typically split on \n), so it also has the ability to remove any blank sequences it encounters
Developer
Posts: 64
Registered: ‎05-09-2009
My Device: Not Specified

Re: "How to split string"

[ Edited ]

I see you're copying the values in a array and return this array. How does this copy work? Are the strings hard copies or still references to the original string to split?

 

...
String[] result = new String[v.size()];
v.copyInto(result);

 

 

Message Edited by swiebertje on 09-23-2009 03:17 PM
Developer
Posts: 183
Registered: ‎12-17-2008
My Device: 8320

Re: "How to split string"

you can try with below code:

 

 

             Original     = A string which you have to split..

             seperation = where your split will be happen.

  

                Vector nodes = new Vector();
                String separator = " ";
               
                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 );
               
                // Create splitted string array
                String[] result = new String[ nodes.size() ];
                if( nodes.size()>0 )
                {
                        for(int loop=0; loop<nodes.size(); loop++)
                        {
                            result[loop] = (String)nodes.elementAt(loop);
                        }
               
                }

kums

******************************

Press kudos which the post help you
Please mark posts as solved if you got a solution
Developer
Posts: 16,987
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: "How to split string"

strings are unmutable objects, every time you change one a new object is created. that is one of the reasons classes like StringBuffer are used.
some information about referencing: http://www.yoda.arachsys.com/java/passing.html

personally i use an own impl of StringTokenizer, with the java source being available it should not be too hard to code.
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Developer
Posts: 112
Registered: ‎07-19-2009
My Device: Not Specified

Re: "How to split string"

public static Vector split(String _strMessage, char _delimiter)
{
StringBuffer strMessage = new StringBuffer(_strMessage);
delimiter = _delimiter;
//Append delimiter to the end of string to mark as string terminator
strMessage.append(delimiter);
int count = countDelimiterInString(strMessage);
Vector messageQueue = new Vector();
for(int i = 0; i < count; i++)
{
messageQueue.addElement(stripFromString(strMessage));
}
return messageQueue;
}
 
 
private static int countDelimiterInString(StringBuffer _strMessage)
{
int count = 0, stringLength, i;
stringLength = _strMessage.length();
char[] str = _strMessage.toString().toCharArray();
for(i = 0; i < stringLength; i++)
{
if(str[i] == delimiter)
{
count++;
}
}
return count;
 
private static String stripFromString(StringBuffer strMessage)
{
char[] str = strMessage.toString().toCharArray();
int stringLength = str.length;
StringBuffer returnStr = new StringBuffer().append("");
int i;
for(i = 0; i < stringLength; i++)
{
if(str[i] == delimiter)
{
strMessage.delete(0, i + 1);
return returnStr.toString();
}
else
{
returnStr.append(str[i]);
}
}
return null;
Regular Visitor
Posts: 1
Registered: ‎11-27-2010
My Device: Not Specified

Re: "How to split string"

I'm sorry, I don't understand why making a such big function.

Here is the split:

 

    public static Vector split(String _strMessage, String _delimiter)
    {
        Vector result = new Vector();
        int pos = 0;
        int len = _strMessage.length();
        int lenD = _delimiter.length();
        for (int i = 0; i < len; i++)
        {
            if (_strMessage.substring(i, i+lenD).equals(_delimiter))
            {
                if (i == pos)
                    result.addElement("");
                else if (i>pos)
                    result.addElement(_strMessage.substring(pos, i));
                pos = i+lenD;
                i += lenD-1;
            }
        }
        if (pos < len)
            result.addElement(_strMessage.substring(pos, len));
        return result;
    }

Have fun