Scala 字符串substring(int beginIndex, int endIndex)方法及示例
substring(int beginIndex, int endIndex)方法用于从所述的String中找到以指定索引开始和结束的子串。
方法的定义:String substring(int beginIndex, int endIndex)
返回类型。返回字符串,它是所述字符串的一部分。
注意:它与subsequence方法相同,但两者之间唯一的区别是subsequence返回CharSequence,而上述方法返回字符串。
例子:1#
// Scala program of substring()
// method
// Creating object
object GfG
{
// Main method
def main(args:Array[String])
{
// Applying substring method
val result = "GeeksforGeeks".substring(4, 8)
// Displays output
println(result)
}
}
输出。
sfor
例如:2#
// Scala program of substring()
// method
// Creating object
object GfG
{
// Main method
def main(args:Array[String])
{
// Applying substring method
val result = "GeeksforGeeks".substring(0, 4)
// Displays output
println(result)
}
}
输出。
Geek
极客教程