带有示例的 Scala String indexOf(String str) 方法
indexOf(String str)方法用于返回在所述字符串中首先出现的子字符串的索引。
Method Definition: indexOf(String str)
Return Type: It returns the index of the sub-string which is specified in the argument of the method.
示例 #1:
// Scala program of int indexOf()
// method
// Creating object
object GfG
{
// Main method
def main(args:Array[String])
{
// Applying indexOf method
val result = "NidhiSingh".indexOf("dh")
// Displays output
println(result)
}
}
输出:
2
示例 #2:
// Scala program of int indexOf()
// method
// Creating object
object GfG
{
// Main method
def main(args:Array[String])
{
// Applying indexOf method
val result = "NidhiSingh".indexOf("Sin")
// Displays output
println(result)
}
}
输出:
5