ios 如何在 Swift 语言中比较两个字符串而忽略大小写?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30532728/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to compare two strings ignoring case in Swift language?
提问by ak_tyagi
How can we compare two strings in swift ignoring case ? for eg :
我们如何在忽略大小写的情况下比较两个字符串?例如:
var a = "Cash"
var b = "cash"
Is there any method that will return true if we compare var a & var b
如果我们比较 var a 和 var b,是否有任何方法会返回 true
采纳答案by Greg
Try this:
尝试这个:
var a = "Cash"
var b = "cash"
let result: NSComparisonResult = a.compare(b, options: NSStringCompareOptions.CaseInsensitiveSearch, range: nil, locale: nil)
// You can also ignore last two parameters(thanks 0x7fffffff)
//let result: NSComparisonResult = a.compare(b, options: NSStringCompareOptions.CaseInsensitiveSearch)
result is type of NSComparisonResult enum:
结果是 NSComparisonResult 枚举类型:
enum NSComparisonResult : Int {
case OrderedAscending
case OrderedSame
case OrderedDescending
}
So you can use if statement:
所以你可以使用 if 语句:
if result == .OrderedSame {
println("equal")
} else {
println("not equal")
}
回答by iAnurag
Try this :
尝试这个 :
For older swift:
对于年长的斯威夫特:
var a : String = "Cash"
var b : String = "cash"
if(a.caseInsensitiveCompare(b) == NSComparisonResult.OrderedSame){
println("voila")
}
Swift 3+
斯威夫特 3+
var a : String = "Cash"
var b : String = "cash"
if(a.caseInsensitiveCompare(b) == .orderedSame){
print("voila")
}
回答by dasblinkenlight
Use caseInsensitiveCompare
method:
使用caseInsensitiveCompare
方法:
let a = "Cash"
let b = "cash"
let c = a.caseInsensitiveCompare(b) == .orderedSame
print(c) // "true"
ComparisonResulttells you which word comes earlier than the other in lexicographic order (i.e. which one comes closer to the front of a dictionary). .orderedSame
means the strings would end up in the same spot in the dictionary
比较结果会告诉您哪个词在词典顺序中比另一个词出现得早(即哪个词更靠近字典的前面)。.orderedSame
意味着字符串将在字典中的同一位置结束
回答by Steve
if a.lowercaseString == b.lowercaseString {
//Strings match
}
回答by matt
Could just roll your own:
可以自己推出:
func equalIgnoringCase(a:String, b:String) -> Bool {
return a.lowercaseString == b.lowercaseString
}
回答by Saurabh Bhatia
CORRECT WAY:
正确方法:
let a: String = "Cash"
let b: String = "cash"
if a.caseInsensitiveCompare(b) == .orderedSame {
//Strings match
}
Please note:ComparisonResult.orderedSame can also be written as .orderedSame in shorthand.
请注意:ComparisonResult.orderedSame 也可以简写为 .orderedSame。
OTHER WAYS:
其他方法:
a.
一种。
if a.lowercased() == b.lowercased() {
//Strings match
}
b.
湾
if a.uppercased() == b.uppercased() {
//Strings match
}
c.
C。
if a.capitalized() == b.capitalized() {
//Strings match
}
回答by cgeek
localizedCaseInsensitiveContains: Returns whether the receiver contains a given string by performing a case-insensitive, locale-aware search
localizedCaseInsensitiveContains:通过执行不区分大小写的区域设置搜索来返回接收器是否包含给定的字符串
if a.localizedCaseInsensitiveContains(b) {
//returns true if a contains b (case insensitive)
}
Edited:
编辑:
caseInsensitiveCompare: Returns the result of invoking compare(_:options:) with NSCaseInsensitiveSearch as the only option.
caseInsensitiveCompare:返回调用 compare(_:options:) 的结果,其中 NSCaseInsensitiveSearch 作为唯一选项。
if a.caseInsensitiveCompare(b) == .orderedSame {
//returns true if a equals b (case insensitive)
}
回答by ikbal
Phone numbers comparison example; using swift 4.2
电话号码比较示例;使用 Swift 4.2
var selectPhone = [String]()
if selectPhone.index(where: {var a = “Cash”
var b = “CASh”
if a.uppercaseString == b.uppercaseString{
//DO SOMETHING
}
.caseInsensitiveCompare(contactsList[indexPath.row].phone!) == .orderedSame}) != nil {
print("Same value")
} else {
print("Not the same")
}
回答by milo526
You could also make all the letters uppercase (or lowercase) and see if they are the same.
您还可以将所有字母大写(或小写),看看它们是否相同。
extension String{
func equalsIgnoreCase(string:String) -> Bool{
return self.uppercaseString == string.uppercaseString
}
}
if "Something ELSE".equalsIgnoreCase("something Else"){
print("TRUE")
}
This will make both variables as ”CASH”
and thus they are equal.
这将使两个变量相同”CASH”
,因此它们相等。
You could also make a String
extension
你也可以做一个String
扩展
extension String {
func compare(_ with : String)->Bool{
return self.caseInsensitiveCompare(with) == .orderedSame
}
}
回答by Mujahid Latif
You can just write your String Extension for comparison in just a few line of code
你可以只用几行代码编写你的字符串扩展来进行比较
##代码##