Swift3.0 language Tutorial comparison, judging string
Swift3.0 Language Tutorials Compare and judge strings, and in a program a lot of strings, often do is to compare and judge these strings. This section explains the content.
1. Case-insensitive comparisons
String comparisons can be divided into three parts: case-insensitive comparisons, case-sensitivity comparisons, and localization comparisons. First we look at case-insensitive comparisons, and case-insensitive comparisons do not distinguish between the case of letters in strings, that is, a and a are the same. Use the Caseinsensitivecompare (_:) method in NSString to implement this function in the following syntax:
Func Caseinsensitivecompare (_ string:string), Comparisonresult
Where string is used to specify a string for comparison. The return value of the method is the Comparisonresult type. Comparisonresult is an enumeration type that contains the following 3 members:
Q orderedascending (-1): small number of left operand less right operand operand.
Q orderedsame (0): Two operands are equal.
Q ordereddescending (1): The left operand is greater than the right operand.
Example 1-29 the following will use the Caseinsensitivecompare (_:) method to compare strings.
Import Foundation
var a=nsstring (string: "Hello")
var b=nsstring (string: "Hello")
var c=nsstring (string: "Swift")
Comparing strings
Print (A.caseinsensitivecompare (b as String). RawValue)
Print (A.caseinsensitivecompare (c as String). RawValue)
Print (C.caseinsensitivecompare (A as String). RawValue)
The results of the operation are as follows:
0
-1
1
2. Case-sensitive comparisons
There is a case-insensitive comparison, and the relative one has a casing comparison. There are 4 methods available in NSString to achieve this comparison, namely compare (_:), compare (_:options:), compare (_:options:range:), and compare (_:options:range: Locale:) method.
(1) The Compare (_:) method is used to compare strings, it needs to distinguish the case of the string, its syntax form is as follows:
Func Compare (_ String:string), Comparisonresult
Where string is used to specify a string for comparison.
"Example 1-30" The following will use the Compare (_:) method to implement a string comparison.
Import Foundation
var a=nsstring (string: "Hello")
var b=nsstring (string: "Hello")
var c=nsstring (string: "Hello")
Comparing strings
Print (A.compare (b as String). RawValue)
Print (A.compare (c as String). RawValue)
Print (B.compare (A as String). RawValue)
The results of the operation are as follows:
-1
0
1
(2) The Compare (_:options:) method is similar to the function implemented by the Compare (_:) method, which compares strings, but it has a mask parameter more than the Compare (_:) method. This parameter can be used to specify an option tag (this tag can be used as a condition for string comparisons). In programming, the Mask parameter is most often set to the following 5 types:
Q caseinsensitive: case-insensitive comparison
Q literal: case-sensitive comparison
Q backwards: Start searching from the end of the string
Q anchored: Search for a restricted range of strings
Q Numeric: According to the numbers in the string, the order is calculated. such as Foo2.txt < Foo7.txt < Foo25.txt
The syntax form of the Compare (_:options:) method is as follows:
Func Compare (_ string:string, Options mask:NSString.CompareOptions = []), Comparisonresul
"Example 1-31" The following will use the Compare (_:options:) method to implement a comparison of strings.
Import Foundation
var a=nsstring (string: "Hello")
var b=nsstring (string: "Hello")
Print (A.compare (b as String, options:NSString.CompareOptions.caseInsensitive). RawValue)// Comparing Strings
The results of the operation are as follows:
0
(3) The Compare (_:options:range:) method is similar to the function of the Compare (_:options:) method, but it is more than compare (_:options:) The Rangeofreceivertocompare method has one more parameter, which can be used to set the comparison range. Its grammatical form is as follows:
Func Compare (_ string:string, Options mask:NSString.CompareOptions = [], range Rangeofreceivertocompare:nsrange), Comparisonresult
Example 1-32 the following will use the Compare (_:options:range:) method to compare strings.
Import Foundation
var a=nsstring (string: "HEllo")
var b=nsstring (string: "Swift")
Print (A.compare (b as String, Options:nsstring.compareoptions.caseinsensitive,range:nsmakerange (1, 3)). RawValue )// Compare Strings
The results of the operation are as follows:
-1
(4) The Compare (_:options:range:locale:) method is similar to the function of the Compare (_:options:range:) method, but it is more than compare (_:options:range:) Method has more than one locale parameter, which can be used to set the locale. Its grammatical form is as follows:
Func Compare (_ string:string, Options mask:NSString.CompareOptions = [], range rangeofreceivertocompare:nsrange, locale : Anyobject?) Comparisonresult
Example 1-33 the following will use the Compare (_:options:range:locale:) method to compare strings.
Import Foundation
var a=nsstring (string: "a")
var b=nsstring (string: "?")
var l=locale.current
Print (A.compare (b as String, options:NSString.CompareOptions.caseInsensitive, Range:nsmakerange (0, 1), locale:l). RawValue)
The results of the operation are as follows:
-1
3. Localization comparison
For localization comparisons, there are 3 methods available in NSString, namely: Localizedcaseinsensitivecompare (_:), Localizedstandardcompare (_:), and Localizedcompare (_:)。
(1) The Localizedcaseinsensitivecompare (_:) method is a case-insensitive, localized comparison string with the following syntax:
Func Localizedcompare (_ string:string), Comparisonresult
Where string is used to specify a string for comparison.
Example 1-34 the following will compare strings using the Localizedcaseinsensitivecompare (_:) method.
Import Foundation
var a=nsstring (string: "Hello")
var b=nsstring (string: "Hello")
var c=nsstring (string: "Swift")
Comparing strings
Print (A.localizedcaseinsensitivecompare (b as String). RawValue)
Print (A.localizedcaseinsensitivecompare (c as String). RawValue)
Print (C.localizedcaseinsensitivecompare (A as String). RawValue)
The results of the operation are as follows:
0
-1
1
(2) The Localizedstandardcompare (_:) method is a comparison of localized standard strings in the following grammatical form:
Func Localizedstandardcompare (_ string:string), Comparisonresult
Where string is used to specify a string for comparison.
"Example 1-35" The following will use the Localizedstandardcompare (_:) method to implement a string comparison.
Import Foundation
var a=nsstring (string: "Hello")
var b=nsstring (string: "Hello")
Print (A.localizedstandardcompare (b as String). RawValue)// Comparing Strings
The results of the operation are as follows:
1
(3) The Localizedcompare (_:) method is a case-sensitive, localized comparison string with the following syntax:
Func Localizedcompare (_ string:string), Comparisonresult
Where string is used to specify a string for comparison.
Example 1-36 the following will use the Localizedcompare (_:) method to implement a comparison of strings.
Import Foundation
var a=nsstring (string: "Hello")
var b=nsstring (string: "Hello")
var c=nsstring (string: "Hello")
Print (A.localizedcompare (b as String). RawValue)
Print (A.localizedcompare (c as String). RawValue)
Print (B.localizedcompare (A as String). RawValue)
The results of the operation are as follows:
1
0
-1
4. Judging the string
In NSString, there are 3 methods for judging strings, Hasprefix (_:), Hassuffix (_:), and IsEqual (to:).
(1) The Hasprefix (_:) method is used to determine whether a string begins with a specified string in the following syntax:
Func Hasprefix (_ str:string), Bool
Where STR is used to specify a string.
Example 1-37 below will determine whether string a begins with "H", "Hel", and "Hele".
Import Foundation
var a=nsstring (string: "Hello")
Determine if string a starts with "H"
if (A.hasprefix ("H")) {
print ("A string is preceded by H")
}else{
print ("A string does not begin with H")
}
Determine if string a starts with "Hel"
if (A.hasprefix ("Hel")) {
print ("A string starts with Hel")
}else{
print ("A string does not start with hel")
}
// Determine if string a starts with "Hele"
if (A.hasprefix ("Hele")) {
print ("A string starts with Hele")
}else{
print ("A string does not start with Hele")
}
The results of the operation are as follows:
A string is preceded by H
A string that starts with Hel
A string does not start with Hele
(2) The Hassuffix (_:) method is exactly the opposite of the Hasprefix (_:) method, which is used to determine whether a string ends with a specified string in the following syntax:
Func Hassuffix (_ str:string), Bool
Where STR is used to specify a string.
Example 1-38 below will determine whether string a ends with "O", "Hello", "Allo".
Import Foundation
var a=nsstring (string: "Hello")
Determines whether string a ends with "o".
if (A.hassuffix ("O")) {
print ("A string is terminated with an O ")
}else{
print ("A string does not end with O ")
}
Determines whether string a ends with "Hello".
if (A.hassuffix ("Hello")) {
print ("A string is terminated with Hello ")
}else{
print ("A string does not end with Hello ")
}
Determines whether string a ends with "Allo".
if (A.hassuffix ("Allo")) {
print ("A string is ending with Allo")
}else{
print ("A string does not end with Allo")
}
The results of the operation are as follows:
A string that ends with an O
A string is the one that ends with Hello
A string does not end with Allo
(3) The IsEqual (to:) method is used to determine whether a string is equal, and its syntax is as follows:
Func isequal (to astring:string), Bool
Where astring is used to specify a string.
Example 1-39 below will determine if the strings are equal.
Import Foundation
var a=nsstring (string: "Hello")
var b=nsstring (string: "Hello")
var c=nsstring (string: "Swift")
var d=nsstring (string: "Hello")
Determine if strings A and B are equal
if (A.isequal (to:b as String)) {
print ("A, b Two strings equal")
}else{
print ("A, b two strings Not equal")
}
Determine if strings A and C are equal
if (A.isequal (to:c as String)) {
print ("A, C two strings equal")
}else{
print ("A, C two strings Not equal")
}
Determine if strings A and d are equal
if (A.isequal (to:d as String)) {
print ("A, D Two strings equal")
}else{
print ("A, D two strings Not equal")
}
The results of the operation are as follows:
A, b two strings are not equal
A, c two strings are not equal
A, d two strings equal
Related reading: Swift3.0 language Tutorial get string encoding and hash address
Swift3.0 language Tutorial comparison, judging string