×

Discussion Board

Results 1 to 2 of 2
  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    6

    Question Problem in splitting the string

    Hi,

    I am working in j2me. I am using the following method to split the string

    static private 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);
    }
    return result;
    }

    This method is split the string very slow. Can anyone help me to make splitting the string fast.


    Thanks in advance
    Ganga

  2. #2
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395

    Re: Problem in splitting the string

    Two optimizations.

    First, avoid splitting the string twice.

    PHP Code:
    public static String[] split(String sString separator) {
        
    Vector vec = new Vector();
        
    int pos 0;
        
    int last 0;
        while ( (
    pos s.indexOf(separatorlast)) >= ) {
            
    vec.addElement(s.substring(lastpos));
            
    last pos 1;
        }
        
    vec.addElement(s.substring(last));
        
    String[] tokens = new String[vec.size()];
        
    vec.copyInto(tokens);
        return 
    tokens;

    By using indexOf(str, fromIndex) we only need one call to substring() instead of two... faster, and less garbage produced.

    Second optimization: if the separator is a single character, we can use indexOf(ch, fromIndex), which can be considerably faster that indexOf(str, fromIndex).

    PHP Code:
    public static String[] split(String schar separator) {
        
    Vector vec = new Vector();
        
    int pos 0;
        
    int last 0;
        while ( (
    pos s.indexOf(separatorlast)) >= ) {
            
    vec.addElement(s.substring(lastpos));
            
    last pos 1;
        }
        
    vec.addElement(s.substring(last));
        
    String[] tokens = new String[vec.size()];
        
    vec.copyInto(tokens);
        return 
    tokens;

    Cheers,
    Graham.

Similar Threads

  1. String encoding problem?
    By Davie1 in forum Symbian
    Replies: 6
    Last Post: 2009-01-23, 12:35
  2. Sms Receving problem
    By cdoewarrior in forum General Messaging
    Replies: 5
    Last Post: 2007-03-03, 15:47
  3. Basic String Manipulation Problem
    By hong.lam in forum Symbian
    Replies: 5
    Last Post: 2006-11-07, 10:51
  4. Problem with String
    By fcobsb in forum Symbian
    Replies: 2
    Last Post: 2003-11-13, 05:32
  5. Problem with Nokia D211 Linux Drivers
    By fiveam in forum Multimodecards
    Replies: 1
    Last Post: 1970-01-01, 02:00

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×