{"id":9904,"date":"2013-03-14T18:05:35","date_gmt":"2013-03-14T12:35:35","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=9904"},"modified":"2024-11-29T00:09:08","modified_gmt":"2024-11-28T18:39:08","slug":"groovy-tokenize-vs-split","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/groovy-tokenize-vs-split\/","title":{"rendered":"Groovy : tokenize() vs split()"},"content":{"rendered":"<p>In the Wild West of text manipulation, Groovy offers two gunslingers for splitting strings: <strong>tokenize()<\/strong> and <strong>split()<\/strong>. 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.<\/p>\n<p>Below are some of the significant differences between both <strong>tokenize()<\/strong> and <strong>split()<\/strong><\/p>\n<table style=\"border-collapse: collapse; width: 100%; height: 120px;\">\n<tbody>\n<tr style=\"height: 24px;\">\n<td style=\"width: 33.3333%; height: 24px; text-align: center;\"><strong>Feature\u00a0<\/strong><\/td>\n<td style=\"width: 33.3333%; height: 24px; text-align: center;\"><strong>tokenize()\u00a0<\/strong><\/td>\n<td style=\"width: 33.3333%; height: 24px; text-align: center;\"><strong>split()<\/strong><\/td>\n<\/tr>\n<tr style=\"height: 24px;\">\n<td style=\"width: 33.3333%; height: 24px; text-align: center;\">Return Type<\/td>\n<td style=\"width: 33.3333%; height: 24px; text-align: center;\">List<\/td>\n<td style=\"width: 33.3333%; height: 24px; text-align: center;\">String[]<\/td>\n<\/tr>\n<tr style=\"height: 24px;\">\n<td style=\"width: 33.3333%; height: 24px; text-align: center;\">Handles Empty Strings<\/td>\n<td style=\"width: 33.3333%; height: 24px; text-align: center;\">Ignores empty tokens<\/td>\n<td style=\"width: 33.3333%; height: 24px; text-align: center;\">Includes empty tokens<\/td>\n<\/tr>\n<tr style=\"height: 24px;\">\n<td style=\"width: 33.3333%; height: 24px; text-align: center;\">Supports Regex Delimiters<\/td>\n<td style=\"width: 33.3333%; height: 24px; text-align: center;\">No<\/td>\n<td style=\"width: 33.3333%; height: 24px; text-align: center;\">Yes<\/td>\n<\/tr>\n<tr style=\"height: 24px;\">\n<td style=\"width: 33.3333%; height: 24px; text-align: center;\">Multi-character Delimiters<\/td>\n<td style=\"width: 33.3333%; height: 24px; text-align: center;\">Each character treated as a delimiter<\/td>\n<td style=\"width: 33.3333%; height: 24px; text-align: center;\">Full string used as delimiter<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<ol>\n<li>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.<\/li>\n<li>\u00a0tokenize(), which returns a list, will ignore an empty string (when a delimiter appears twice in succession), whereas split() keeps such string.\n<pre>[java]\r\nString testString = 'hello brother'\r\nassert testString.split() instanceof String[]\r\nassert ['hello','brother']==testString.split() \/\/split with no arguments\r\nassert['he','','o brother']==testString.split('l')\r\n\/\/ split keeps empty string\r\nassert testString.tokenize() instanceof List\r\nassert ['hello','brother']==testString.tokenize() \/\/tokenize with no arguments\r\nassert ['he','o brother']==testString.tokenize('l')\r\n\/\/tokenize ignore empty string\r\n[\/java]<\/pre>\n<\/li>\n<li>The tokenize() method uses each character of a String as a delimiter whereas split()\u00a0 takes the entire string as a delimiter\n<pre>[java]\r\nString testString='hello world'\r\nassert ['hel',' world']==testString.split('lo')\r\nassert ['he',' w','r','d']==testString.tokenize('lo')\r\n[\/java]<\/pre>\n<\/li>\n<li>The split() can take regex as a delimiter whereas tokenize does not.\n<pre>[java]\r\nString testString='hello world 123 herload'\r\nassert['hello world ',' herload']==testString.split(\/\\d{3}\/)\r\n[\/java]<\/pre>\n<p>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!<\/p>\n<p>Feel free to ask our <a title=\"grails developer\" href=\"http:\/\/www.tothenew.com\/grails-application-development\" target=\"_blank\" rel=\"noopener\">grails developers<\/a>, if you have any queries<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>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\u00a0 tokenize()\u00a0 [&hellip;]<\/p>\n","protected":false},"author":69,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":5950},"categories":[7],"tags":[],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/9904"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/69"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=9904"}],"version-history":[{"count":3,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/9904\/revisions"}],"predecessor-version":[{"id":68846,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/9904\/revisions\/68846"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=9904"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=9904"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=9904"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}