How to Create a subsequence in Scala? Scala String Subsequence() Method

Here, we will see the subsequence() method on string in Scala. Subsequence method is used to find the sub-sequence from the given string.
Submitted by Shivang Yadav, on January 27, 2021

String is an immutable collection that stores sequences of characters.

String subsequence() Method

The subsequence() method on strings in Scala is used to create a sub-sequence from the string from the startingIndex to endingIndex.

Syntax:

string_Name.subsequence(startingIndex, endingIndex)

Parameters: The method takes two parameters, one starting index of the subsequence and the other one ending index of the subsequence.

Return Value: It returns a string that is the subsequence created from the string.

Example 1: Creating a substring with the given starting and ending index

object MyClass {
    def main(args: Array[String]) {
        val myString = "ScalaProgrammingLanguage"
        println("The string is " + myString)

        println("Creating a subString with starting index = 0 and ending index = 5")

        val SS = myString.subSequence(0, 5)
        println("The substring is '" + SS + "'")
    }
}

Output:

The string is ScalaProgrammingLanguage
Creating a subString with starting index = 0 and ending index = 5
The substring is 'Scala'

Example 2:

object MyClass {
    def main(args: Array[String]) {
        val myString = "ScalaProgrammingLanguage"
        println("The string is " + myString)
    
        println("Creating a subString...")
    
        val SS = myString.subSequence(5, 20)
        println("The substring is '" + SS + "'")
    }
}

Output:

The string is ScalaProgrammingLanguage
Creating a subString...
The substring is 'ProgrammingLang'




Comments and Discussions




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.