def sampleText = "The quick brown fox" println sampleText.tokenize()
The sample above will break the String using whitespace to separate items. Hence we get four items. This will print:
[The, quick, brown, fox]
Space, tabs and carriage return are whitespaces. The code below will yield the same output because \t or tab, and \n or next line are valid whitespaces:
def sampleText = "The quick\tbrown\nfox" println sampleText.tokenize()Extra white space will not affect the result.
def sampleText = "The quick \n\n\n brown fox" println sampleText.tokenize()
Will have the same 4 items:
[The, quick, brown, fox]
def sampleText = "The quick brown fox" println sampleText.tokenize() instanceof String[] println sampleText.tokenize() instanceof ListWill return false for the first line because the result is not an array of String, and true for the second because it is actually an instance of a List:
false true
def sampleText = "no-one-leaves" println sampleText.tokenize("-")
The code will print:
[no, one, leaves]
It will only use the given delimiter and treat whitespace as part of the resulting items, example:
def sampleText = "a no-a one-a leaves" println sampleText.tokenize("-")
Will not use space as delimiter, but the dash only. Hence, we get items:
[a no, a one, a leaves]
If the delimiter is more than one character, it treats each character as a delimiter. For example:
def sampleText = "pen pineapple apple pen" println sampleText.tokenize(" p")By passing space and p, the String is tokenized either by space or the letter p. It will not look for exactly space followed by a p, but the occurance of either letter will delimit the String. Hence the output will be:
[en, inea, le, a, le, en]
Split by a single digit
def sampleText = "A1B23C456D" println sampleText.tokenize(/\d/)The regular expression means any digit, but the tokenize will not separate the String given any digit. The result will be:
[A1B23C456D]Because regular expression does not work with tokenize(). You may want to look at Groovy String's split() method instead if you want to use regular expressions.