Scala

Scala Substring function

Scala is a high-performance functional language that offers various functions for string manipulation. The substring function is one of them. We can get the specific part of a string from the Scala string() function in Scala. The Scala string() function is a built-in function in Scala, so we can directly access this without importing any libraries and packages in our Scala codes. In the article, we will have a demonstration of the fundamental Scala string() function. Let’s get started to have some more information on the Scala string() function.

What is Substring() in Scala:

As substring is the subset of string between two indexes. Substring() functions are used to get the specified part of a string from a given string in Scala. However, we have a defined string that starts and ends specified index.

Syntax of the Substring() function in Scala:

The general syntax of the Scala substring() is given below:

Here “string” is the specified string from which we will get a substring that is represented as “substring”. The function substring() in Scala takes two parameters as an argument. First is the “int beginIndex” which is the starting index point of the substring function in Scala, and the second one is “int endIndex” which is the ending point of the substring() function in Scala.

From the Scala substring() function, we have a return value of substring that is the part of the string at specified index on the beginning point and specified index at the endpoint.

How to use Substring() in Scala:

To understand the basics of using the substring() function in Scala, we should take a look at the examples:

Example # 1: Using int BeginIndex in Substring() function in Scala

In the example, we will pass an “int BeginIndex” argument that is the starting point of a specified index of a substring from a given string. Let’s have the execution of the code and see as it will return the substring at the end of the input string. For this, we have an example code below.

In the above program, we have declared a Scala class with the name “example”, and in our main() function of a class, we have created a variable “var” with the name “new_str” that is basically an input string followed by calling Scala substring() function. We have passed a value “20” as the starting index for the substring, so it starts from index 10 and returns the result at the end of the string. Here we are just mentioning the beginning index, not the end index. We are printing the “new_str” string to get the substring.

As you can see, we have the end index substring from the input string that is shown below on the terminal screen.

Example # 2: Using int endIndex in Substring() function in Scala

As we have mentioned above in the syntax of the Scala substring() function, that it takes two parameters now in the example program, we are taking both the parameter as an argument that is “int beginIndex” and “int endIndex”. These parameters will act as the starting and ending indexes of the substring. For instance, we can see the example program for better understanding.

In this example, we have a Scala class as “substr” which defines our main() function. In the main() function, we have the “str1” variable as a string, and we are concatenating “str1” with other strings through print statements. We have the “str2” variable here in which we are calling substring() function and assigned begin index as “0” and end index as “10” that get us as a substring in between these index values. Here also we are concatenating the “str2” with the other string. We will get a substring and a modified string by printing the strings.

The output of both the string “str1” and “str2” is displayed below.

Example # 3: Using type parameter of Substring() function in Scala:

We are taking one negative scenario of the substring() function in Scala to check whether the value string takes the type parameter or not. We will get to know this after executing the particular example program. See the program of the example below.

As in the above code, we have a class “substr” that defines the main() function for our class. The main function has a variable with “str1” that inputs a string here. Then we have another variable, “str2” with having a string. Note that in the last print statement, we are passing “str2” with the type parameter with “2”. Let’s have a look at executing the example.

The output on execution show type parameter error which shows that the value substring of type string does take a type parameter

Example # 4: Using two specified positions in Substring() function in Scala:

This example program gets a substring from a given string from two specified positions. We have two positions starting point and the ending point that will take a substring between those assigned positions. We will have a practice example below.

First, declaring the substring() class in scala and named that class with “mystring”. Secondly, we have our main() function that stores all the programs of the class. In the main function, we have assigned a variable to a string that represents “mystr” and side by side; we are printing the strings. Then we have four strings with the names “str1”, “str2”, “str3” and “str4” with a start and end index, which will get the substring by using the substring() function in Scala.

Displaying the implementation of the above program, we have an output for each specified index value.

Conclusion:

As we have a brief demonstration of the Substring() function in Scala, how we can extract the substring from a given string with the example executions on Ubuntu 20.04. It will always return a new string on execution. As we have no issue while using the Scala substring() function because it is a built-in function in Scala language that is easy to use and more efficient than java. This will definitely give you hands-on while using the substring() function in Scala.

About the author

Saeed Raza

Hello geeks! I am here to guide you about your tech-related issues. My expertise revolves around Linux, Databases & Programming. Additionally, I am practicing law in Pakistan. Cheers to all of you.