Updated April 20, 2023
Definition of Scala Split
As the name suggest split is used to split the string in Scala. In a programming language, we have a requirement where we want to split our long string based on some regular expression or any special character, any character, space, ‘,’ (comma) for this purpose we have split method in Scala available. We can call this method on any string. After splitting the string, it will return us the array contain all the elements present inside it. The split function also divides the string into several parts. In the coming section, we will discuss more about the method and its usage in real life for a better understanding.
Syntax:
This method is used to split our string in Scala. We can call this method on any string object. Let’s see the syntax for better understanding of the methods how we can use this while programming. See below;
1. With limit pass as a parameter
split(String regular_expression, int limit)
In the above syntax we are passing two parameters as the input in Scala. The first one is the regular expression and other one is limit.
2. Without limit param
split(String regular_expression)
In the above syntax, we are passing only one parameter the input for the split method without specifying the limit here. In the coming section, we will discuss more about its practice usage for better understanding and hands on.
How does Split Function Work in Scala?
As now we know that split function is used to split the string, We can split string based on some regular expression or any any special character or any substring we want to give. We can pass any expression there. We have two syntax for this method one is with specify the limit and another one is without specifying the limit for the split method. Limit parameter will limit the number of values in the array. Now we can discuss more about the method signature and their return type as well. Which are as follow see below;
- split(String regular_expression): This method takes two parameter as the input. By using this parameter, we can specify the string by which we want to split our string. We can specify any character, substring, any regular expression also accepted here. The return type for this method is array only, it will return us new array containing the parameter after the split.
- split(String regular_expression, int limit): This method is also used to split the string based on the regular expression. But this method takes two parameters as the input. The first parameter is the regularexpression on the basis of this string would be spitted. The second parameter about this we will discuss in detail see below;
- limit: This parameter is used to specify the limit of values in the final array. In short, it just restricts our resultant array to some number only. Below we will see one practice example of how we can specify this parameter in the method see below;
Example:
split("hello", 5);
In this way we can specify the limit parameter in the split function, it will only limit the values inside the array to 5 here.
Let’s see one practice example for split method to understand its internal working and how we can use this in our program see below;
Example:
object Main extends App{
// Your code here
println("XXXXXXXX")
var mystr = "samplestringcontinuestring"
val finalresult = mystr.split("string")
}
In the above lines of code, we are trying to split our string. First we are creating a string named as ‘mystr’, on this we are calling the split method. Inside the methods, we are mentioning ‘string’ as the split expression for our string. So whenever the ‘string’ word will appear in our variable it will split the string and divide them into several parts and skip that string. So in the above case output will be a sample, continue because we are splitting it on basis of the ‘string’ keyword.
In the same way we can specify the limit parameter to limit our output array. In this, if we limit our parameter as ‘1’ then only it will print the ‘sample’ string for us as the output.
Examples of Scala Split
In this example, we are trying to split our string in several parts. Here we are using the split function with and without the limit parameter, some of the string is split by using the substring, some based on the space, and limit is also applied here.
Example #1
Code:
object Main extends App{
// Your code here
println("Demo for split method in Scala !!! ")
var mystr1 = "samplestringcontinuestring"
var mystr2= "mystrytotestthesplit"
var mystr3 = "demoexmapleto test"
var mystr4 = "smaple string to test spilt method with limit"
val finalresult1 = mystr1.split("the")
val finalresult2 = mystr2.split("the")
val finalresult3 = mystr3.split(" ")
val finalresult4 = mystr4.split(" ", 5)
println("Result for each string would be ::")
for ( r1 <-finalresult1 )
{
println(r1)
}
println("*****************************")
for ( r2 <-finalresult2 )
{
println(r2)
}
println("*****************************")
for ( r3 <-finalresult3 )
{
println(r3)
}
println("*****************************")
for ( r4 <-finalresult4 )
{
println(r4)
}
}
Output:
Example #2
In this example we are using limit parameter with the split method here so this will restrict the resultant array also the value in the array will be limited here.
Code:
object Main extends App{
// Your code here
println("Demo for split method in Scala !!! ")
var mystr1 = "samplestringcontinuestring"
var mystr2= "mystrytotestthesplit"
var mystr3 = "demoexmapleto test"
var mystr4 = "sample string to test spilt method with limit"
val finalresult1 = mystr1.split("the", 0)
val finalresult2 = mystr2.split("the", 2)
val finalresult3 = mystr3.split(" ", 1)
val finalresult4 = mystr4.split(" ", 5)
println("Result for each string would be ::")
for ( r1 <-finalresult1 )
{
println(r1)
}
println("*****************************")
for ( r2 <-finalresult2 )
{
println(r2)
}
println("*****************************")
for ( r3 <-finalresult3 )
{
println(r3)
}
println("*****************************")
for ( r4 <-finalresult4 )
{
println(r4)
}
}
Output:
Conclusion
Split is used to divide the string into several parts containing inside an array because this function return us array as result. We can also limit our array elements by using the ‘limit’ parameter. Otherwise, it will just contain all the parameters from the string that has been spitted.
Recommended Articles
We hope that this EDUCBA information on “Scala Split” was beneficial to you. You can view EDUCBA’s recommended articles for more information.