TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to SCALA

Scala Split String Examples

Call split to separate strings on delimiters. Use an Array for multiple delimiter characters.
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
Scala program that uses split // The string we want to split. val line = "brick,cement,concrete" // Split on each comma. val result = line.split(',') // Array length. println(result.length) // Print all elements in array. for (v <- result) { println(v) } Output 3 brick cement concrete
Multiple delimiters. Sometimes a string may have more than one delimiter char. This becomes complex, but using multiple delimiters to split can help.

Here: 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.

Scala program that uses split with multiple delimiters // This has several delimiters. val codes = "abc;def,ghi:jkl" // Use an Array argument to split on multiple delimiters. val result = codes.split(Array(';', ',', ':')) // Print result length. println(result.length) // Display all elements in the array. result.foreach(println(_)) Output 4 abc def ghi jkl
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.
Scala program that uses substring delimiter // Strings are separated with two-char delimiters. val equipment = "keyboard; mouse; screen" // Split on substring. val result = equipment.split("; ") result.foreach(println(_)) Output keyboard mouse screen
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.

Scala program that uses split with Regex // This string has an empty item between delimiters. val items = "box; ; table; chair" // Use a Regex to split the string. // ... Any number of spaces or semicolons is a delimiter. val result = items.split("[; ]+") // Print our results. // ... No empty elements are present. for (r <- result) { println(r) } Output box table chair
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.
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf