{Pjy{dMy{S^[M^{i8pUwM[{y{SjMyS{iM^[M^{=8pUM@[{Oy{dPMyM@[{Py{YjP9EMy{iM98|M[kDbbIErrX-~B| 6A966VCC 6A9566V 6A94VC 6A94G5V

Split.` Strings can contain sentences. But often they contain lists of structured data. Fields are separated by commas (or other characters). Split helps us process this data.`With split,` a Scala method that acts on StringLike values, we specify a delimiter or many delimiters. The method returns an array. We can process the string's fields in this array.`An example.` Here we use just one delimiter char, a comma. Our constant string "line" has three parts to it—these are separated by commas. We call split. `Array: `The split def returns an array of strings. This has 3 elements. We call println on the three elements in a for-loop.`For `for-scala`Multiple delimiters.` Sometimes a string may have more than one delimiter char. This becomes complex, but using multiple delimiters to split can help. `We call split and pass an Array argument. The elements are the characters we want to split on (the delimiters).`Result: `The various delimiters are handled correctly by split. The alphabetical strings are returned in the array.`Substring delimiter.` Sometimes we want to split on a longer delimiter (like two chars). Here we split on a two-char substring. We print the results. `Regex.` A regular expression can separate a string. This can remove empty entries—we can treat two delimiters as one. Here we treat any number of spaces and semicolons as a delimiter. `Result: `The empty string between delimiters in the "items" string is ignored. The two surrounding delimiters are combined.`A summary.` In Scala we find methods that act on StringLike types. Split is one of these functions. We invoke it to separate structured data within a block of text.

567 76The 6 we want6split. 7val line67"brick,cement,concrete"7 66 on each comma. 7val 66line.7split7(',')7 66 l6. 76ln(6.l6)7 6Pr6all 6s66. 76(v <- 6) { 56ln(v) } 7 3 brick cement concrete7 76This has several delimiters. 7val codes67"abc;def,ghi:jkl"7 6Use an 6 argument6split on multiple delimiters. 7val 66codes.7split7(767(';', ',', ':'))7 6Pr66 l6. 76ln(6.l6)7 66 all 6s6the 6. 76.6(6ln(_)) 7 4 abc def ghi jkl7 766s are separated with two-char delimiters. 7val equipment67"keyboard; mouse; screen"7 66 on sub6. 7val 66equipment.7split7(7"; "7) 6.6(6ln(_)) 7 keyboard mouse screen7 76This 6 has an empty item between delimiters. 7val items67"box; ; table; chair"7 6Use a 66split the 6. 6Any 6 of spaces or semicolons is a delimiter. 7val 66items.7split7(7"[; ]+"7)7 6Pr6our 6s. 6No empty 6s are present. 767 (r <- 6) { 56ln(r) } 7 box table chair7

$Unc`splitsplit with multiple delimiterssubstring delimitersplit with Regex