如何以特定数字访问字符串中的字符? [英] How can I access a char in string in at specific number?

查看:15
本文介绍了如何以特定数字访问字符串中的字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试了这段代码,但它给了我错误.那么如何在 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 the charAt() method.

private fun abc(x: String) {
    var i: Int = 0
    while (x[i].toString() != "+") {
        var y: Char = x[i]
        i++
    }
}

推荐答案

相当于Javas String.charAt()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)

这篇关于如何以特定数字访问字符串中的字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆