带有示例的 Scala String charAt() 方法
charAt()方法用于返回参数中传递的索引的字符。
Method Definition: char charAt(int index)
Return Type: It returns char for the index given in the argument.
示例 #1:
// Scala program of charAt()
// method
// Creating object
object GfG
{
// Main method
def main(args:Array[String])
{
// Creating a String
val m1 = "Nidhi"
// Applying charAt() method
val result = m1.charAt(2)
// Displays output
println(result)
}
}
输出:
d
示例 #2:
// Scala program of charAt()
// method
// Creating object
object GfG
{
// Main method
def main(args:Array[String])
{
// Creating a String
val m1 = "Nidhi"
// Applying charAt() method
val result = m1.charAt(0)
// Displays output
println(result)
}
}
输出:
N