kDbIErrX-~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.

EOIHCIICO{The OP we wantOjsplit.ICval lineOyCHbrick,cement,concreteHCIIO{Od on each comma.ICval OMOyline.CsplitC(',')CIIO{OS lO^.ICO[ln(OM.lO^)CIIO{PrOiall O8sOpOU.ICOw(v <- OM) {IEO[ln(v)I}IICII3IbrickIcementIconcreteCIICO{This has several delimiters.ICval codesOyCHabc;def,ghi:jklHCIIO{Use an OS argumentOjsplit on multiple delimiters.ICval OMOycodes.CsplitC(COSC(';', ',', ':'))CIIO{PrOiOM lO^.ICO[ln(OM.lO^)CIIO{O= all O8sOpthe OU.ICOM.O@(O[ln(_))IICII4IabcIdefIghiIjklCIICO{OOs are separated with two-char delimiters.ICval equipmentOyCHkeyboard; mouse; screenHCIIO{Od on subOP.ICval OMOyequipment.CsplitC(CH; HC)IOM.O@(O[ln(_))IICIIkeyboardImouseIscreenCIICO{This OP has an empty item between delimiters.ICval itemsOyCHbox; ; table; chairHCIIO{Use a OYOjsplit the OP.IO9Any OE of spaces or semicolons is a delimiter.ICval OMOyitems.CsplitC(CH[; ]+HC)CIIO{PrOiour OMs.IO9No empty O8s are present.ICO|C (r <- OM) {IEO[ln(r)I}IICIIboxItableIchairC

$Unc`splitsplit with multiple delimiterssubstring delimitersplit with Regex