The requirement is to split strings in Java so that the following
“this#{s}is#{s}a#{s}string”
would result in the following array
[“this”,”is”,”a”,”string”]
As you can see here the delimiter is the character sequence “#{s}”.
What is the fastest and efficient way of doing this using existing tools?
Am I right to assume that using regex (String.split()) is a bit of wasting because we are splitting using static string?
I got the assumption from here http://www.javamex.com/tutorials/regular_expressions/splitting_tokenisation_performance.shtml .
But I cannot use StringTokenizer since the delimiter is a sequence of char.
Note: currently I’m using String.split() and have no problem with that. This is pure curiosity.
Faster than using
String.splitisPattern.split: i.e., precompile the pattern and store that for subsequent use. If you use the same pattern all the time, and do a lot of splitting using that pattern, it may be worth putting that pattern into a static field or something.Also, if your pattern contains no regex metacharacters, you can pass in
Pattern.LITERALwhen creating the pattern. This is something you can’t do withString.split. 😛