如何以特定编号访问字符串中的char? [英] How can i access a char in string in at specific number?
本文介绍了如何以特定编号访问字符串中的char?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我尝试了这段代码,但它给了我错误. 因此,如何在Kotlin中访问字符串中的字符. 在Java中,可以通过charAt()方法完成.
I tried this code but it is giving me errors. So how can i access a character in a string in kotlin. in java it can be done by charAt() method.
private fun abc(x: String) {
var i: Int = 0
while (x[i].toString() != "+") {
var y: Char = x[i]
i++
}
}
推荐答案
等效于Java String.get().由于这是作为运算符实现的,因此可以使用[index]
代替get(index)
.例如
The equivalent of Javas String.charAt() in Kotlin is String.get(). Since this is implemented as an operator, you can use [index]
instead of get(index)
. For example
val firstChar: Char = "foo"[0]
或者,如果您愿意
val someString: String = "bar"
val firstChar: Char = someString.get(0)
这篇关于如何以特定编号访问字符串中的char?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文