Groovy : tokenize() vs split()

14 / Mar / 2013 by Vivek Garg 3 comments
  1. The split() method returns a string [] instance and the  tokenize() method returns a list instance
  2.  tokenize() ,which returns a list, will ignore empty string (when a delimiter appears twice in  succession) where as split() keeps such string.

    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

  3. The tokenize() method uses each character of a String as delimiter where as split()  takes the entire string as delimiter

    String testString=’hello world’
    assert [‘hel’,’ world’]==testString.split(‘lo’)
    assert [‘he’,’ w’,’r’,’d’]==testString.tokenize(‘lo’)

  4. The split() can take regex as delimiter where as tokenize does not.

    String testString=’hello world 123 herload’
    assert[‘hello world ‘,’ herload’]==testString.split(/\d{3}/)

    Feel free to ask our grails developers, if you have any queries


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.

  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.


Leave a Reply

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