如何将其转换为Swift:
NSString *searchString = [NSString stringWithFormat:@"%@", @"Apple_HFS "];
NSRange range = [tempString rangeOfString:searchString];
NSUInteger *idx = range.location + range.length;
由于
答案 0 :(得分:1)
这就是你要找的东西吗?
var str : NSString = "A string that include the searched string Apple_HFS inside"
let searchString : NSString = "Apple_HFS "
let range : NSRange = str.rangeOfString(searchString) // (42, 10)
let idx : Int = range.location + range.length // 52
答案 1 :(得分:1)
如果您使用String
,则可以引用endIndex
:
let searchString: String = "Apple_HFS "
if let range: Range<String.Index> = tempString.rangeOfString(searchString) {
let index = range.endIndex
let stringAfter = tempString.substringFromIndex(index)
// do something with `stringAfter`
} else {
// not found
}
我包含了类型,因此您可以看到正在发生的事情,但通常我只会写:
let searchString = "Apple_HFS "
if let range = tempString.rangeOfString(searchString) {
let stringAfter = tempString.substringFromIndex(range.endIndex)
// do something with `stringAfter`
} else {
// not found
}
答案 2 :(得分:0)
简单解决方案,不使用任何objc方法或类型:
假设你有:
let nsrange = NSRange(location: 3, length: 5)
let string = "My string"
现在您需要将NSRange
转换为Range
:
let range = Range(start: advance(string.startIndex, nsrange.location), end: advance(string.startIndex, nsrange.location + nsrange.length))