我有一个字符串,我想在该字符串中找到每个用户所选单词的位置。例如,如果用户选择"我的",我希望每次都能找到"我的"在字符串中找到。目前我正在使用:
var range = stringContent?.rangeOfString("my")
println(range?.location)
不幸的是,这只给了我第一个" my"在字符串中。我怎样才能找到所有" my" s的位置?谷歌没有太大的帮助 - 所有的建议和例子都找到了这个词的第一个例子。我想要所有的实例。
感谢您的帮助!
答案 0 :(得分:1)
这基本上是@Fogmeister建议的,但是使用NSString的内置方法enumerateSubstringsInRange(_:options:usingBlock:)。它将为您提供“my”出现的范围数组。
var rangesOfMatches = [NSRange]()
someString.enumerateSubstringsInRange(NSMakeRange(0, someString.length), options: .ByWords) { (substring, substringRange, _, _) in
if substring == "my" {
rangesOfMatches.append(substringRange)
}
}
答案 1 :(得分:0)
好的,现在在Swift ......
let string: NSString = "my blah my blah my"
let searchString = "my"
var range: NSRange = string.rangeOfString(searchString)
while range.location != NSNotFound {
println(range)
let searchRange = NSMakeRange(range.location + range.length, string.length - range.length - range.location)
range = string.rangeOfString(searchString, options: nil, range: searchRange)
}
这打印......
(0,2)
(8,2)
(16,2)
字符串“my”的正确位置。
答案 2 :(得分:0)
您可以使用NSScanner
扫描字符串,记录索引,扫描字符串,冲洗并重复:
let scanner = NSScanner(string: "This is a test.")
while !scanner.atEnd {
scanner.scanUpToString("is", intoString: nil)
println(scanner.scanLocation) // will print 2 the first time around, and 5 the second
scanner.scanString("is", intoString: nil)
}
这应该为您提供要搜索的字符串的每个实例的起始索引。
但请注意,如果它的使用方式与此完全相同,它还会记录字符串的结束索引。放弃。
答案 3 :(得分:0)
如果要使用纯Swift字符串和范围,则需要在循环中执行此操作:
var stringContent = "amydamydamydamydamydamydamydamydamydamydamydamyd"
var index = stringContent.startIndex
while let range = stringContent.rangeOfString("my", options: NSStringCompareOptions.allZeros, range: Range(start: index, end: stringContent.endIndex)) {
index = range.endIndex.successor()
println(index)
}
答案 4 :(得分:0)
Swift的另一个解决方案,我发现subString& Swift中的String.Index !!!
let myString = "gffffsfjjjjjjfsfffffffsjjjjfs"
let subString = "fs"
let strLen = countElements(myString)
//init search range : the whole string
var searchRange = Range(start:advance(myString.startIndex,0), end: advance(myString.startIndex, strLen))
var foundRange = myString.rangeOfString(subString, options: NSStringCompareOptions.CaseInsensitiveSearch, range: searchRange, locale: nil)
var i: Int = 0;
while ((foundRange?.isEmpty) != nil) {
i++
//recaculate searchRange
searchRange = Range(start:advance(foundRange!.endIndex ,0), end: advance(myString.startIndex, strLen))
foundRange = myString.rangeOfString(subString, options: NSStringCompareOptions.CaseInsensitiveSearch, range: searchRange, locale: nil)
}
NSLog("Count = \(i)")
//扩展版
extension String {
func countOccurent(_subString : String) -> Int {
let strLen = countElements(self)
//init search range : the whole string
var searchRange = Range(start:advance(self.startIndex,0), end: advance(self.startIndex, strLen))
var foundRange = self.rangeOfString(_subString, options: NSStringCompareOptions.CaseInsensitiveSearch, range: searchRange, locale: nil)
var i: Int = 0;
while ((foundRange?.isEmpty) != nil) {
i++
//re-caculate searchRange
searchRange = Range(start:advance(foundRange!.endIndex ,0), end: advance(self.startIndex, strLen))
foundRange = self.rangeOfString(_subString, options: NSStringCompareOptions.CaseInsensitiveSearch, range: searchRange, locale: nil)
}
NSLog("Result = \(i)")
return i
}
}
答案 5 :(得分:0)
我的swift-fu缺乏,但这是Obj-C中的一个选项。 (我很想知道这是如何转换为swift的 - 我无法获得NSCountedSet的构造函数来获取数组......)
NSUInteger count = [[[NSCountedSet alloc] initWithArray: [@"hello world hello again" componentsSeparatedByString: @" "]] countForObject: @"hello"];