Home »
Scala
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'