带有示例的 Scala String substring(int beginIndex, int endIndex) 方法
substring(int beginIndex, int endIndex)方法用于从指定的字符串中找到以指定索引开始和结束的子字符串。
Method Definition: String substring(int beginIndex, int endIndex)
Return Type: It returns string which is the part of the stated String.
注意:与 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