无法在 Swift 2 中字符串 var 的字符的位置和索引处找到字符 [英] Cannot find char at position and index of char of a string var in Swift 2
问题描述
Swift 2 中 Java 的 charAt() 和 indexOf() 的等效函数?
Equivalent functions in Swift 2 of Java's charAt() and indexOf() ?
推荐答案
您是否阅读了 NSString 文档?String
和 NSString
不相同,但正如 this 所提到的回答大多数功能或多或少相同.无论如何,您要求的两个确切功能就在那里:
Did you read the NSString documentation? String
and NSString
are not identical, but as mentioned by this answer most functions are more or less the same. Anyway, the two exact functions you ask for are right there:
Java 的charAt
:
func characterAtIndex(_ index: Int) -> unichar
Java 的indexOf
:
func rangeOfString(_ searchString: String) -> NSRange
NSRange
数据类型只包含 2 个变量:location
和 length
,它们告诉您子字符串在原始字符串中的位置和长度这是.请参阅 文档.
The NSRange
data type contains just 2 variables: location
and length
that tell you where the substring is in the original string and how long it is. See the documentation.
//Java
"abc".indexOf("b") => 1
//Swift
"abc".rangeOfString("b").location #=> 1
这篇关于无法在 Swift 2 中字符串 var 的字符的位置和索引处找到字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!