-
-
Save jskorpan/1056060 to your computer and use it in GitHub Desktop.
Ultra 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 { | |
private static ThreadLocal<String[]> tempArray = new ThreadLocal<String[]>(); | |
public static String[] tokenize(String string, char delimiter) | |
{ | |
String[] temp = tempArray.get(); | |
int tempLength = (string.length() / 2) + 2; | |
if (temp == null || temp.length < tempLength) | |
{ | |
temp = new String[tempLength]; | |
tempArray.set(temp); | |
} | |
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
why/how is this ultra fast?