×

Scala Tutorial

Scala Basics

Scala Control statements

Scala Functions

Scala Arrays

Scala Lists

Scala Strings

Scala Classes & Objects

Scala Collections

Scala String Subsequence() Method

By IncludeHelp Last updated : October 21, 2024

Description and Usage

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!

Load comments ↻





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