Scala String Programs
- How to determine if a string contains a regular expression in Scala?
- Scala program to split string
- Scala program to convert string to integer
- How to count the number of characters in a string in Scala?
- How to create a range of characters in Scala?
- How to left-trim and right-trim strings in Scala?
- How to get date, month and year as string or number in Scala?
- How to replace a regular expression pattern in a string in Scala?
- Extracting difference, intersection, and distinct characters between two strings in Scala
- How to convert a string to byte array in Scala?
- How to convert byte array to string in Scala?
- How to convert a string to date in Scala?
- How to convert hex string to int in Scala?
- How to convert hex string to long in Scala?
- How to convert Int to Double in Scala?
- Scala program to compare strings using equal to (==) operator
- Scala program to compare strings using equals() method
- Scala program to compare strings using compareTo() method
- Scala program to compare strings using equalsIgnoreCase() method
- Scala program to concatenate strings using the plus (+) operator
- Scala program to concatenate strings using the concat() method
- Scala program to extract the substring from a string
- Scala program to find the length of the string
- Scala program to get characters from the string
- Scala program to check the string starts with a specified substring
- Scala program to check the string ends with specified substring
- Scala program to get bytes array from string using getBytes() method
- Scala program to copy the content of string into a character array
- Scala program to copy the content of string into a character array
- Scala program to convert the string into a character array
- Scala program to convert an integer number into a string
- Scala program to convert a floating-point number into a string
- Scala program to get the hash code of the string
- Scala program to get the index of the first occurrence of the specified substring
- Scala program to get the index of the last occurrence of the specified substring
- Scala program to replace the character with another character in the string
- Scala program to demonstrate the replaceAll() method
- Scala program to demonstrate the replaceFirst() method
- Scala program to split the string based on the given separator
- Scala program to demonstrate the subSequence() method
- Scala program to convert the string into lowercase
- Scala program to convert the string into uppercase
- Scala program to trim a string
- Scala program to concatenate string using \'s\' method of string interpolation
- Scala program to concatenate formatted string using \'f\' method of string interpolation
- Scala program to print raw string using the raw method of string interpolation
- Scala program to create a string using StringBuilder class
- Scala program to append a character to an immutable string using the \'+=\' operator
- Scala program to append a string to an immutable string using the \'++=\' operator
- Scala program to append a number to an immutable string using append() method
- Scala program to clear the content of StringBuilder string
- Scala program to delete a substring from StringBuilder string
- Scala program to insert a substring into StringBuilder string
- Scala program to replace a substring into StringBuilder string
- Scala program to get the length of StringBuilder string
- Scala program to get characters from StringBuilder string
- Scala program to check whether a string is empty or not
- Scala program to check nullable string
- Scala program to create a string using StringBuffer class
- Scala program to demonstrate the append() method with StringBuffer class
- Scala program to delete a substring from a string created using StringBuffer
- Scala program to insert a substring into string created using StringBuffer
Program to split a string in Scala
Output
This method is useful when we are working with data that are encoded in a string like URL-encoded string, multiline data from servers, etc where the string need to be split to extract information. One major type is when data comes in the form of CSV (comma-separated value) strings which is most common and needs each value to be separated from the string.
Let's see how to,
Program to split a CSV string using split method
Output
Splitting String using Regular Expressions
In the split method, a regular expression(regex) can also be used as a separator to split strings.
Output
Here, we have separated the string using the regex \\d+ which considers any digit. In our string, the function will split every time a digit is encountered.
need an explanation for this answer? contact us directly to get an explanation for this answer