是时候承认失败了……

在Objective-C中,我可以使用如下内容:

NSString* str = @"abcdefghi";
[str rangeOfString:@"c"].location; // 2

在Swift中,我看到了类似的东西:

var str = "abcdefghi"
str.rangeOfString("c").startIndex

...但这只是给了我一个字符串。索引,我可以使用它下标回原始字符串,但不能从中提取位置。

FWIW,字符串。Index有一个名为_position的私有ivar,其中有正确的值。我只是不明白怎么会暴露出来。

我知道我自己可以很容易地将其添加到String中。我更好奇在这个新的API中我缺少了什么。


当前回答

如果你想使用熟悉的NSString,你可以显式地声明它:

var someString: NSString = "abcdefghi"

var someRange: NSRange = someString.rangeOfString("c")

我还不确定如何在Swift中做到这一点。

其他回答

仔细想想,你其实并不需要位置的确切Int版本。范围甚至是字符串。如果需要,Index足以再次获取子字符串:

let myString = "hello"

let rangeOfE = myString.rangeOfString("e")

if let rangeOfE = rangeOfE {
    myString.substringWithRange(rangeOfE) // e
    myString[rangeOfE] // e

    // if you do want to create your own range
    // you can keep the index as a String.Index type
    let index = rangeOfE.startIndex
    myString.substringWithRange(Range<String.Index>(start: index, end: advance(index, 1))) // e

    // if you really really need the 
    // Int version of the index:
    let numericIndex = distance(index, advance(index, 1)) // 1 (type Int)
}
    // Using Swift 4, the code below works.
    // The problem is that String.index is a struct. Use dot notation to grab the integer part of it that you want: ".encodedOffset"
    let strx = "0123456789ABCDEF"
    let si = strx.index(of: "A")
    let i = si?.encodedOffset       // i will be an Int. You need "?" because it might be nil, no such character found.

    if i != nil {                   // You MUST deal with the optional, unwrap it only if not nil.
        print("i = ",i)
        print("i = ",i!)            // "!" str1ps off "optional" specification (unwraps i).
            // or
        let ii = i!
        print("ii = ",ii)

    }
    // Good luck.
extension String{
    func contains(find: String)->Bool{
        return self.range(of: find) != nil
    }
}
 
func check(n:String, h:String)->Int{
    let n1 = n.lowercased()
    let h1 = h.lowercased()//lowercase to make string case insensitive
    var pos = 0 //postion of substring
    if h1.contains(n1){
       // checking if sub string exists
        if let idx = h1.firstIndex(of:n1.first!){
             let pos1 = h1.distance(from: h1.startIndex, to: idx)
           pos = pos1
        }
        return pos
    }
    else{
        return -1
    }
}
 
print(check(n:"@", h:"hithisispushker,he is 99 a good Boy"))//put substring in n: and string in h

我不确定如何从字符串中提取位置。索引,但如果你愿意回到一些Objective-C框架,你可以桥接到Objective-C,用和以前一样的方式。

"abcdefghi".bridgeToObjectiveC().rangeOfString("c").location

看起来有些NSString方法还没有(或者可能不会)移植到String中。包含也出现在脑海中。

斯威夫特5

查找子字符串的索引

let str = "abcdecd"
if let range: Range<String.Index> = str.range(of: "cd") {
    let index: Int = str.distance(from: str.startIndex, to: range.lowerBound)
    print("index: ", index) //index: 2
}
else {
    print("substring not found")
}

查找字符索引

let str = "abcdecd"
if let firstIndex = str.firstIndex(of: "c") {
    let index: Int = str.distance(from: str.startIndex, to: firstIndex)
    print("index: ", index)   //index: 2
}
else {
    print("symbol not found")
}