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