我现在正在学习 Dart,我想知道如何在字符串中找到字母。我试过使用 charAt()
,但随后 Dart 编辑器说:
Class 'String' has no instance method 'charAt'.
所以我的问题是:如何在 Dart 的字符串中找到字母?
例如,我想找到
"i"
在字"fire"
.如果没有 charAt()
,这如何工作?var items = "fire";
for (int i = 0; items.length; i++) {
if (items.indexOf(items(charAt(i)) != -1) {
//..
}
}
最佳答案
正如评论中所说,自 indexOf / allMatches / contains 起,您不必创建自己的函数在大多数情况下足够安静。
并且没有 charAt() 等效项,因为 String 充当字符列表。您可以使用 [] operator在特定索引处获取字母:
'Hello World'[6] //return 'W'
关于string - 在字符串中查找字母 (charAt),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23477845/