Updated April 4, 2023
Introduction to Scala Substring
As the name suggests, a scala substring is used to get the substring from the given input. We can find out the substring by specifying any index. Substring function can be call on any string. But have to pass the index in order to use the substring method in Scala. There may be some requirement where we need to find the substring present inside the string input itself so we can go with the substring method in scala. We have two types of methods available in scala to get the substring from the given string. In the coming section, we will discuss more about this method in detail.
Syntax
As substring string is a method available in scala used to get the substring from the specified index, Let’s see its syntax for better understanding of it and how to use this method on programming see below;
substring(int index)
As we can see in the above syntax, this method takes one parameter as the input type. We can call this method on any String input. Let’s see one practice syntax to understand its real usage see below;
val demostr = "some string here"
demostr.substring(3)
How does substring work in Scala?
As of now, we know that the substring method is used to get the string from the string in scala. It is an in build method available in scala, so we can directly use this without being involved in any library in our program. But we can find out the string by mentioning the index while using this method. This method takes one parameter as the input. We have two substring method available in scala based on the input parameter we pass. Now we will see each of them in details about the method signature and its return type; for more understanding, see below;
Method signature
1. String substring(int begningIndex): This is the method signature of substring function as per the scala doc. In this method, we are passing one parameter as the input here. Let’s discuss the input param in detail see below;
- int begningIndex: This parameter is used to specify the index of the substring that needs to get from the string input. This is the beginning index of the substring so that it will return the string at the end of the input string from the beginning index. For instance, we can see one example to for better understanding see below;
Example:
object Main extends App{
// Your code here
val str = "hellotoallbye".substring(10)
println(str)
}
In the above code lines, we create one string named ‘str’ followed by calling the substring method on it. But here, we are passing ’10’ as the starting index for the substring. So it will start from 10 and return us the result at the end of the string. Here we are not mentioning any end index.
2. String substring(int begningIndex, int endIndex): In this method, we are passing two input parameter, and this method is also used to get the substring from the given string input. Only the method signature is different; we can call it method overloading in terms of object-oriented programming language. Let’s discuss each of the parameters in details for better understanding see below;
- int begningIndex: This parameter issued to assign the value which will act as the starting point of the substring method in scala. It will work in the same way as before without any difference.
- int endIndex: This parameter issued to specify the end index for the substring. Which means it will return the substring at this end index only. This index will act as the ending point for the substring where our substring will end and complete. We will see one practice example of how to use this method while programming see below;
Example:
object Main extends App{
// Your code here
val str = "hellotoallbye".substring(8, 10)
println(str)
}
As you can see in the above lines of code, we are creating one string here and calling the substring method, but this time it will return as the string between the specified index only. This will act as the starting and endpoint of the substring here.
3. return type: Scala’s substring method always returns a new string between the specified index of the given string.
Examples
Here are the following examples of Scala Substring mention below:
Example #1
In this example, we are calculating the string from the beginning index only using substring in scala.
Code:
object Main extends App{
// Your code here
val str1 = "i am string one as input"
val str2 = "i am string two as input"
val str3 = "i am string three as input"
val str4 = "i am string four as input"
val str5 = "i am string five as input"
val result1 = str1.substring(13)
val result2 = str2.substring(11)
val result3 = str3.substring(2)
val result4 = str4.substring(5)
val result5 = str5.substring(15)
println("Result one is :: " )
println(result1)
println("Result two is :: " )
println(result2)
println("Result three is :: " )
println(result3)
println("Result four is :: " )
println(result4)
println("Result five is :: " )
println(result5)
}
Output:
Example #2
In this example, we are using the start and end index to get the substring using the substring in scala.
Code:
object Main extends App{
// Your code here
val str1 = "i am string one as input"
val str2 = "i am string two as input"
val str3 = "i am string three as input"
val str4 = "i am string four as input"
val str5 = "i am string five as input"
val result1 = str1.substring(12, 16)
val result2 = str2.substring(12, 16)
val result3 = str3.substring(12, 16)
val result4 = str4.substring(12, 16)
val result5 = str5.substring(12, 16)
println("Result one is :: " )
println(result1)
println("Result two is :: " )
println(result2)
println("Result three is :: " )
println(result3)
println("Result four is :: " )
println(result4)
println("Result five is :: " )
println(result5)
}
Output:
Conclusion
By the use of it, we can get the substring from the given string. It will always return us a new string. Also, it is the build method available in scala, so it is easy to use and readable like any other programming language like java.
Recommended Articles
We hope that this EDUCBA information on “Scala Substring” was beneficial to you. You can view EDUCBA’s recommended articles for more information.