Created
May 22, 2011 10:28
-
-
Save cgbystrom/985337 to your computer and use it in GitHub Desktop.
Super-fast Java string tokenizer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
May throw ArrayIndexOutOfBoundsException on line 14 during certain circumstances, looking into it.
About 10 times faster than anything else provided by the SDK anyhow