Groovy : tokenize() vs split()
In the Wild West of text manipulation, Groovy offers two gunslingers for splitting strings: tokenize() and split(). They might seem like two sides of the same coin, but choosing the right one can make your code smoother than a well-oiled six-shooter.
Below are some of the significant differences between both tokenize() and split()
Feature | tokenize() | split() |
Return Type | List | String[] |
Handles Empty Strings | Ignores empty tokens | Includes empty tokens |
Supports Regex Delimiters | No | Yes |
Multi-character Delimiters | Each character treated as a delimiter | Full string used as delimiter |
- Both methods split a string into tokens but with subtle nuances. The split() method returns a string [] instance, while the tokenize() method returns a list instance.
- tokenize(), which returns a list, will ignore an empty string (when a delimiter appears twice in succession), whereas split() keeps such string.
[java] String testString = 'hello brother' assert testString.split() instanceof String[] assert ['hello','brother']==testString.split() //split with no arguments assert['he','','o brother']==testString.split('l') // split keeps empty string assert testString.tokenize() instanceof List assert ['hello','brother']==testString.tokenize() //tokenize with no arguments assert ['he','o brother']==testString.tokenize('l') //tokenize ignore empty string [/java]
- The tokenize() method uses each character of a String as a delimiter whereas split() takes the entire string as a delimiter
[java] String testString='hello world' assert ['hel',' world']==testString.split('lo') assert ['he',' w','r','d']==testString.tokenize('lo') [/java]
- The split() can take regex as a delimiter whereas tokenize does not.
[java] String testString='hello world 123 herload' assert['hello world ',' herload']==testString.split(/\d{3}/) [/java]
So, next time you need to split strings in Groovy, remember these gunslingers. Choose the one that fits your task and shoot down those stringy dilemmas with ease!
Feel free to ask our grails developers, if you have any queries
It’s spelled “delimiter”.
Hi there, ϒoս’vе doone a fantastіс job. I’ll definitely digg it and
personally recommmend to my friends. I am sure they’ll be benefited from this website.
En este lugar podrás localizar trucos para juegos de tu consola
preferida, Ordenador, Playstation, Playstation dos, PSP, Xbox,
Xbox 360, Nintendo DS. Registrate y publica los trucos que conozcas de tus juegos preferidos de cualquiera
de las consolas de nuestra sección de trucos
y trampas para juegos (Ordenador, Playstation, Playstation 2,
PSP, Xbox, Xbox 360, Nintendo DS). Acá los cuentas con, listos para oír y descargar para
tu teléfono móvil.