在Objective-C中,检查NSString子字符串的代码是:
NSString *string = @"hello Swift";
NSRange textRange =[string rangeOfString:@"Swift"];
if(textRange.location != NSNotFound)
{
NSLog(@"exists");
}
但是如何在Swift中做到这一点呢?
在Objective-C中,检查NSString子字符串的代码是:
NSString *string = @"hello Swift";
NSRange textRange =[string rangeOfString:@"Swift"];
if(textRange.location != NSNotFound)
{
NSLog(@"exists");
}
但是如何在Swift中做到这一点呢?
当前回答
Xcode 8/Swift 3版本:
let string = "hello Swift"
if let range = string.range(of: "Swift") {
print("exists at range \(range)")
} else {
print("does not exist")
}
if let lowercaseRange = string.lowercased().range(of: "swift") {
print("exists at range \(lowercaseRange)")
} else {
print("does not exist")
}
你也可以使用contains:
string.contains("swift") // false
string.contains("Swift") // true
其他回答
扩展的方法
斯威夫特4
extension String {
func contains(find: String) -> Bool{
return self.range(of: find) != nil
}
func containsIgnoringCase(find: String) -> Bool{
return self.range(of: find, options: .caseInsensitive) != nil
}
}
var value = "Hello world"
print(value.contains("Hello")) // true
print(value.contains("bo")) // false
print(value.containsIgnoringCase(find: "hello")) // true
print(value.containsIgnoringCase(find: "Hello")) // true
print(value.containsIgnoringCase(find: "bo")) // false
通常Swift 4有包含方法,但它可从iOS 8.0+
斯威夫特3.1
你可以为字符串写扩展contains:和containsIgnoringCase
extension String {
func contains(_ find: String) -> Bool{
return self.range(of: find) != nil
}
func containsIgnoringCase(_ find: String) -> Bool{
return self.range(of: find, options: .caseInsensitive) != nil
}
}
旧Swift版本
extension String {
func contains(find: String) -> Bool{
return self.rangeOfString(find) != nil
}
func containsIgnoringCase(find: String) -> Bool{
return self.rangeOfString(find, options: NSStringCompareOptions.CaseInsensitiveSearch) != nil
}
}
例子:
var value = "Hello world"
print(value.contains("Hello")) // true
print(value.contains("bo")) // false
print(value.containsIgnoringCase("hello")) // true
print(value.containsIgnoringCase("Hello")) // true
print(value.containsIgnoringCase("bo")) // false
给你:
let s = "hello Swift"
if let textRange = s.rangeOfString("Swift") {
NSLog("exists")
}
另一个。支持大小写和变音符。
斯威夫特3.0
struct MyString {
static func contains(_ text: String, substring: String,
ignoreCase: Bool = true,
ignoreDiacritic: Bool = true) -> Bool {
var options = NSString.CompareOptions()
if ignoreCase { _ = options.insert(NSString.CompareOptions.caseInsensitive) }
if ignoreDiacritic { _ = options.insert(NSString.CompareOptions.diacriticInsensitive) }
return text.range(of: substring, options: options) != nil
}
}
使用
MyString.contains("Niels Bohr", substring: "Bohr") // true
iOS 9 +
iOS 9开始提供大小写和变音符不敏感功能。
if #available(iOS 9.0, *) {
"Für Elise".localizedStandardContains("fur") // true
}
Xcode 8/Swift 3版本:
let string = "hello Swift"
if let range = string.range(of: "Swift") {
print("exists at range \(range)")
} else {
print("does not exist")
}
if let lowercaseRange = string.lowercased().range(of: "swift") {
print("exists at range \(lowercaseRange)")
} else {
print("does not exist")
}
你也可以使用contains:
string.contains("swift") // false
string.contains("Swift") // true
在Xcode 7.1和Swift 2.1中,containsString()为我工作得很好。
let string = "hello swift"
if string.containsString("swift") {
print("found swift")
}
斯威夫特4:
let string = "hello swift"
if string.contains("swift") {
print("found swift")
}
一个不区分大小写的Swift 4示例:
let string = "Hello Swift"
if string.lowercased().contains("swift") {
print("found swift")
}
或者使用不区分大小写的字符串扩展名:
extension String {
func containsIgnoreCase(_ string: String) -> Bool {
return self.lowercased().contains(string.lowercased())
}
}
let string = "Hello Swift"
let stringToFind = "SWIFT"
if string.containsIgnoreCase(stringToFind) {
print("found: \(stringToFind)") // found: SWIFT
}
print("string: \(string)")
print("stringToFind: \(stringToFind)")
// console output:
found: SWIFT
string: Hello Swift
stringToFind: SWIFT