Home » Scala language

Operation on Strings in Scala

There are various operations that are done on the strings in Scala. In this tutorial, we will learn all the string operations that can be done along with their syntax and working example.
Submitted by Shivang Yadav, on July 03, 2019

Scala strings operation

A string is a very important datatype in Scala. This is why there are a lot of operations that can be done on the string object. Since the regular operations like addition, subtraction is not valid for a string, therefore, special operations like concatenation, comparison are defined.

In this tutorial, we will share some of the important and common string functions.

1) String Equality (==)

The tradition equality operator == is also available in the string. You can use it to find equality of two string and the equality results in a boolean value.

    str1 = str2 // this will return either TRUE OR FALSE 

For Example,

    val str1 = "Include"
    val str2 = "Includes"
    str1 == str2 // this will be false

2) String Length

There is an inbuilt function that is used to find the number of characters in a string. It includes all the space that comes it between. It is used to set the limit for string traversal.

    // this outputs a positive integer denoting the 
    // number of characters in the array. 
    str1.length 

For example,

    val str1 = "Include help"
    str1.length // this will print 12

3) String concat

You can concatenate a string on other. It means the string will be appended on the calling string.

    str1.concat(str2)

for example,

    val str1 = "Include"
    val str2 = "Help"
    str1.concat(str2) // This will print IncludeHelp

4) String charAt() method

To print the character at a specific index of the string the charAt method is used. The input is a zero-based index and output will be the corresponding character. Will throw an error if the input is greater than the length of the string.

    str1.charAt(n)

For example,

    val name = "Include"
    name.charAt(4) // This will output u.

5) indexOf() method

The indexOf() method is used to check the index of a character in a string. This method returns an integer which is positive within the length of the string when the character is found in the array otherwise -1 is given as output.

    str1.indexOf("a")

For example,

    val name = "Include"
    name.indexOf("d") // This will output 5.

6) Substring() method

The substring method is used to define a substring from the calling string. It makes a new string with the specified part of the string.

    str2 =  str1.substring(startIndex , endIndex)

For example,

    val name = "IncludeHelp is awesome"
    name.substring(14 , 21) // This will output awesome.

Example code that uses all these functions

object MyClass {
      
      def main(args: Array[String]) {
         val str1 = "include Help "
         val str2 = "is awesome"
         
         println("str1 = " + str1)
         println("str2 = " + str2)
         
         println("Comparison between str1 and str2 is " + (str1 == str2))
         println("The length of str1 is " + str1.length)
         println("Concatenating str1 with str2 gives \n " + str1.concat(str2))
         println("The character at  index 5 of str2 is" + str2.charAt(5))
         println("The index of 'c' in str1 is " + str1.indexOf("c"))
      }
   }

Output

str1 = include Help 
str2 = is awesome
Comparison between str1 and str2 is false
The length of str1 is 13
Concatenating str1 with str2 gives 
 include Help is awesome
The character at  index 5 of str2 ise
The index of 'c' in str1 is 2


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.