| Author |
split vs substring
|
fahad siddiqui
Ranch Hand
Joined: Jun 14, 2006
Posts: 85
|
|
Which would give me a better performance advantage? for substring, i would be first checking the indexOf and then subtringing in 2 parts. The string would break to a max of 2 parts.
|
 |
Jeanne Boyarsky
internet detective
Marshal
Joined: May 26, 2003
Posts: 23181
|
|
|
Neither is likely to be time consuming enough to be a bottleneck. Go with the one that is clearer and easier to maintain - in this case split().
|
[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Certs: SCEA Part 1, Part 2 & 3 & Core Spring 3
|
 |
rajesh bala
Ranch Hand
Joined: Jan 14, 2003
Posts: 66
|
|
split() internally creates Pattern.compile which is an expensive operation. so I would say, use split() in an efficient way. Or use Pattern.compile(regex).split(this, limit); directly. ~Rajesh.B
|
 |
Ilja Preuss
author
Sheriff
Joined: Jul 11, 2001
Posts: 14112
|
|
|
I'm with Jeanne here - simply use split() until it is proven to be a bottleneck for you application.
|
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
|
 |
rajesh bala
Ranch Hand
Joined: Jan 14, 2003
Posts: 66
|
|
FYI... This is the snippet from String.java. public String[] split(String regex, int limit) { return Pattern.compile(regex).split(this, limit); } If you already know what you are trying to split, its always better to compile the regex and compile the pattern only once. ~Rajesh.B
|
 |
 |
|
|
subject: split vs substring
|
|
|