Skip to content

Instantly share code, notes, and snippets.

@cgbystrom
Created May 22, 2011 10:28
Show Gist options
  • Save cgbystrom/985337 to your computer and use it in GitHub Desktop.
Save cgbystrom/985337 to your computer and use it in GitHub Desktop.
Super-fast Java string tokenizer
public class StringTokenizer {
public static String[] tokenize(String string, char delimiter) {
String[] temp = new String[(string.length() / 2) + 1];
int wordCount = 0;
int i = 0;
int j = string.indexOf(delimiter);
while( j >= 0) {
temp[wordCount++] = string.substring(i, j);
i = j + 1;
j = string.indexOf(delimiter, i);
}
temp[wordCount++] = string.substring(i);
String[] result = new String[wordCount];
System.arraycopy(temp, 0, result, 0, wordCount);
return result;
}
}
@jskorpan
Copy link

May throw ArrayIndexOutOfBoundsException on line 14 during certain circumstances, looking into it.

About 10 times faster than anything else provided by the SDK anyhow

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment