Ios 快速启动方法?
Swift中是否有startsWith()方法或类似的方法 我基本上是在检查某个字符串是否以另一个字符串开头。我还希望它不区分大小写 正如你可能知道的,我只是想做一个简单的搜索功能,但我似乎在这方面失败得很惨 这就是我想要的: 输入“sa”应该可以得到“圣安东尼奥”、“圣达菲”等的结果。 输入“SA”或“SA”甚至“SA”也应返回“圣安东尼奥”或“圣达菲” 我在用Ios 快速启动方法?,ios,swift,Ios,Swift,Swift中是否有startsWith()方法或类似的方法 我基本上是在检查某个字符串是否以另一个字符串开头。我还希望它不区分大小写 正如你可能知道的,我只是想做一个简单的搜索功能,但我似乎在这方面失败得很惨 这就是我想要的: 输入“sa”应该可以得到“圣安东尼奥”、“圣达菲”等的结果。 输入“SA”或“SA”甚至“SA”也应返回“圣安东尼奥”或“圣达菲” 我在用 self.rangeOfString(find, options: NSStringCompareOptions.CaseInsen
self.rangeOfString(find, options: NSStringCompareOptions.CaseInsensitiveSearch) != nil
在iOS9之前,它工作得很好。然而,升级到iOS9后,它停止工作,现在搜索区分大小写
var city = "San Antonio"
var searchString = "san "
if(city.rangeOfString(searchString, options: NSStringCompareOptions.CaseInsensitiveSearch) != nil){
print("San Antonio starts with san ");
}
var myString = "Just a string with san within it"
if(myString.rangeOfString(searchString, options: NSStringCompareOptions.CaseInsensitiveSearch) != nil){
print("I don't want this string to print bc myString does not start with san ");
}
编辑:为Swift 3更新 Swift String类确实有区分大小写的方法
hasPrefix()
,但是如果需要不区分大小写的搜索,可以使用NSString方法范围(of:options:)
注意:默认情况下,NSString方法不可用,但如果您导入基础,则它们是可用的
因此:
选项可以是不区分大小写
或[.case不区分大小写]
。如果要使用其他选项,可以使用第二个选项,例如:
let range = city.range(of: searchString, options:[.caseInsensitive, .backwards])
这种方法还有一个优点,就是可以在搜索中使用其他选项,例如
.diacriticInsensitive
搜索。仅仅使用无法获得相同的结果。字符串上的小写()
extension String {
func startsWith(string: String) -> Bool {
guard let range = rangeOfString(string, options:[.AnchoredSearch, .CaseInsensitiveSearch]) else {
return false
}
return range.startIndex == startIndex
}
}
用法示例:
var str = "Hello, playground"
let matches = str.startsWith("hello") //true
let no_matches = str.startsWith("playground") //false
使用hasPrefix
而不是startsWith
例如:
"hello dolly".hasPrefix("hello") // This will return true
"hello dolly".hasPrefix("abc") // This will return false
Swift 3版本:
func startsWith(string: String) -> Bool {
guard let range = range(of: string, options:[.caseInsensitive]) else {
return false
}
return range.lowerBound == startIndex
}
要具体回答大小写不敏感的前缀匹配问题,请执行以下操作:
纯Swift(大多数情况下推荐)
或:
注意:对于空前缀“
两种实现都将返回true
利用基础
注意:对于空前缀“
它将返回false
和regex一起变丑(我见过…)
注意:对于空前缀“
它将在swift 4中返回false(使用possiblePrefix:possiblePrefix)->Bool,其中possiblePrefix:Sequence,String.Element==possiblePrefix.Element
将被引入
示例用法:
let a = 1...3
let b = 1...10
print(b.starts(with: a))
// Prints "true"
在带扩展的Swift 4中
我的扩展示例包含3个函数:选中do a Stringstartwith a subString、do a Stringendto a subString和do a Stringcontainsa subString
如果要忽略字符“A”或“A”,请将isCaseSensitive参数设置为false,否则将其设置为true。
有关其工作原理的更多信息,请参见代码中的注释
代码:
import Foundation
extension String {
// Returns true if the String starts with a substring matching to the prefix-parameter.
// If isCaseSensitive-parameter is true, the function returns false,
// if you search "sA" from "San Antonio", but if the isCaseSensitive-parameter is false,
// the function returns true, if you search "sA" from "San Antonio"
func hasPrefixCheck(prefix: String, isCaseSensitive: Bool) -> Bool {
if isCaseSensitive == true {
return self.hasPrefix(prefix)
} else {
var thePrefix: String = prefix, theString: String = self
while thePrefix.count != 0 {
if theString.count == 0 { return false }
if theString.lowercased().first != thePrefix.lowercased().first { return false }
theString = String(theString.dropFirst())
thePrefix = String(thePrefix.dropFirst())
}; return true
}
}
// Returns true if the String ends with a substring matching to the prefix-parameter.
// If isCaseSensitive-parameter is true, the function returns false,
// if you search "Nio" from "San Antonio", but if the isCaseSensitive-parameter is false,
// the function returns true, if you search "Nio" from "San Antonio"
func hasSuffixCheck(suffix: String, isCaseSensitive: Bool) -> Bool {
if isCaseSensitive == true {
return self.hasSuffix(suffix)
} else {
var theSuffix: String = suffix, theString: String = self
while theSuffix.count != 0 {
if theString.count == 0 { return false }
if theString.lowercased().last != theSuffix.lowercased().last { return false }
theString = String(theString.dropLast())
theSuffix = String(theSuffix.dropLast())
}; return true
}
}
// Returns true if the String contains a substring matching to the prefix-parameter.
// If isCaseSensitive-parameter is true, the function returns false,
// if you search "aN" from "San Antonio", but if the isCaseSensitive-parameter is false,
// the function returns true, if you search "aN" from "San Antonio"
func containsSubString(theSubString: String, isCaseSensitive: Bool) -> Bool {
if isCaseSensitive == true {
return self.range(of: theSubString) != nil
} else {
return self.range(of: theSubString, options: .caseInsensitive) != nil
}
}
}
示例如何使用:
import Foundation
extension String {
// Returns true if the String starts with a substring matching to the prefix-parameter.
// If isCaseSensitive-parameter is true, the function returns false,
// if you search "sA" from "San Antonio", but if the isCaseSensitive-parameter is false,
// the function returns true, if you search "sA" from "San Antonio"
func hasPrefixCheck(prefix: String, isCaseSensitive: Bool) -> Bool {
if isCaseSensitive == true {
return self.hasPrefix(prefix)
} else {
var thePrefix: String = prefix, theString: String = self
while thePrefix.count != 0 {
if theString.count == 0 { return false }
if theString.lowercased().first != thePrefix.lowercased().first { return false }
theString = String(theString.dropFirst())
thePrefix = String(thePrefix.dropFirst())
}; return true
}
}
// Returns true if the String ends with a substring matching to the prefix-parameter.
// If isCaseSensitive-parameter is true, the function returns false,
// if you search "Nio" from "San Antonio", but if the isCaseSensitive-parameter is false,
// the function returns true, if you search "Nio" from "San Antonio"
func hasSuffixCheck(suffix: String, isCaseSensitive: Bool) -> Bool {
if isCaseSensitive == true {
return self.hasSuffix(suffix)
} else {
var theSuffix: String = suffix, theString: String = self
while theSuffix.count != 0 {
if theString.count == 0 { return false }
if theString.lowercased().last != theSuffix.lowercased().last { return false }
theString = String(theString.dropLast())
theSuffix = String(theSuffix.dropLast())
}; return true
}
}
// Returns true if the String contains a substring matching to the prefix-parameter.
// If isCaseSensitive-parameter is true, the function returns false,
// if you search "aN" from "San Antonio", but if the isCaseSensitive-parameter is false,
// the function returns true, if you search "aN" from "San Antonio"
func containsSubString(theSubString: String, isCaseSensitive: Bool) -> Bool {
if isCaseSensitive == true {
return self.range(of: theSubString) != nil
} else {
return self.range(of: theSubString, options: .caseInsensitive) != nil
}
}
}
要检查字符串是否以“TEST”开头:
要检查字符串是否以“test”开头:
检查字符串是否以“G123”结尾:
检查字符串是否以“g123”结尾:
要检查字符串是否包含“RING12”:
要检查字符串是否包含“ring12”:
您能否给出一个具体的例子,说明rangeOfString和CaseInsensitiveSearch在哪些方面不能按预期工作?我已经在iOS 9模拟器中对它进行了测试,它对我有效。OP要求不区分大小写,您的答案是区分大小写。在比较之前,使用“string”可以很容易地将字符串设置为小写。使用.archored
,小写()可以更快。请看我的答案或奥利弗·阿特金森的答案。
extension String {
func caseInsensitiveHasPrefix(_ prefix: String) -> Bool {
return range(of: prefix, options: [.anchored, .caseInsensitive]) != nil
}
}
extension String {
func caseInsensitiveHasPrefix(_ prefix: String) -> Bool {
guard let expression = try? NSRegularExpression(pattern: "\(prefix)", options: [.caseInsensitive, .ignoreMetacharacters]) else {
return false
}
return expression.firstMatch(in: self, options: .anchored, range: NSRange(location: 0, length: characters.count)) != nil
}
}
let a = 1...3
let b = 1...10
print(b.starts(with: a))
// Prints "true"
import Foundation
extension String {
// Returns true if the String starts with a substring matching to the prefix-parameter.
// If isCaseSensitive-parameter is true, the function returns false,
// if you search "sA" from "San Antonio", but if the isCaseSensitive-parameter is false,
// the function returns true, if you search "sA" from "San Antonio"
func hasPrefixCheck(prefix: String, isCaseSensitive: Bool) -> Bool {
if isCaseSensitive == true {
return self.hasPrefix(prefix)
} else {
var thePrefix: String = prefix, theString: String = self
while thePrefix.count != 0 {
if theString.count == 0 { return false }
if theString.lowercased().first != thePrefix.lowercased().first { return false }
theString = String(theString.dropFirst())
thePrefix = String(thePrefix.dropFirst())
}; return true
}
}
// Returns true if the String ends with a substring matching to the prefix-parameter.
// If isCaseSensitive-parameter is true, the function returns false,
// if you search "Nio" from "San Antonio", but if the isCaseSensitive-parameter is false,
// the function returns true, if you search "Nio" from "San Antonio"
func hasSuffixCheck(suffix: String, isCaseSensitive: Bool) -> Bool {
if isCaseSensitive == true {
return self.hasSuffix(suffix)
} else {
var theSuffix: String = suffix, theString: String = self
while theSuffix.count != 0 {
if theString.count == 0 { return false }
if theString.lowercased().last != theSuffix.lowercased().last { return false }
theString = String(theString.dropLast())
theSuffix = String(theSuffix.dropLast())
}; return true
}
}
// Returns true if the String contains a substring matching to the prefix-parameter.
// If isCaseSensitive-parameter is true, the function returns false,
// if you search "aN" from "San Antonio", but if the isCaseSensitive-parameter is false,
// the function returns true, if you search "aN" from "San Antonio"
func containsSubString(theSubString: String, isCaseSensitive: Bool) -> Bool {
if isCaseSensitive == true {
return self.range(of: theSubString) != nil
} else {
return self.range(of: theSubString, options: .caseInsensitive) != nil
}
}
}
"testString123".hasPrefixCheck(prefix: "TEST", isCaseSensitive: true) // Returns false
"testString123".hasPrefixCheck(prefix: "TEST", isCaseSensitive: false) // Returns true
"testString123".hasPrefixCheck(prefix: "test", isCaseSensitive: true) // Returns true
"testString123".hasPrefixCheck(prefix: "test", isCaseSensitive: false) // Returns true
"testString123".hasSuffixCheck(suffix: "G123", isCaseSensitive: true) // Returns false
"testString123".hasSuffixCheck(suffix: "G123", isCaseSensitive: false) // Returns true
"testString123".hasSuffixCheck(suffix: "g123", isCaseSensitive: true) // Returns true
"testString123".hasSuffixCheck(suffix: "g123", isCaseSensitive: false) // Returns true
"testString123".containsSubString(theSubString: "RING12", isCaseSensitive: true) // Returns false
"testString123".containsSubString(theSubString: "RING12", isCaseSensitive: false) // Returns true
"testString123".containsSubString(theSubString: "ring12", isCaseSensitive: true) // Returns true
"testString123".containsSubString(theSubString: "ring12", isCaseSensitive: false) // Returns true