Groovy : tokenize() vs split()

14 / Mar / 2013 by Vivek Garg 3 comments

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()

  1. 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
  2.  tokenize(), which returns a list, will ignore 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]
  3. 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]
  4. 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

FOUND THIS USEFUL? SHARE IT

comments (3)

  1. delsea drive in

    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.

    Reply
  2. excandeciéndole

    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.

    Reply

Leave a Reply to Blaine Cancel reply

Your email address will not be published. Required fields are marked *