50% OFF!!!

Friday, August 28, 2009

J2ME String Split method

Here is my code for splitting a string under Java - J2ME.
this is very helpfull for using a string array instead of string manipulation.

This is the best & fastest splitting method

Code:

public static String[] split(String p_text, String p_seperator)
{
Vector vecStrings = new Vector();

int index;
int prevIdx = 0;

while ((index = p_text.indexOf(p_seperator, prevIdx)) > -1)
{
vecStrings.addElement(p_text.substring(prevIdx, index));
prevIdx = index + 1;
}
vecStrings.addElement(p_text.substring(prevIdx));

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

return result;
}

2 comments:

  1. Code is excellent. Working fine.Thanks for your solution.

    ReplyDelete
  2. The line:

    prevIdx = index + 1;

    works only for a single-character separator. It should be replaced with:

    prevIdx = index + p_seperator.length();

    ReplyDelete