NSString Compare strings, I introduce some common methods:
NSString *value = @ "1234567890";
Methods of comparison:
[Value Compare:(NSString *)];
[Value Compare:(NSString *) Options:(nsstringcompareoptions)];
[Value Compare:(NSString *) Options:(nsstringcompareoptions) range:(nsrange)];
Parameters passed in:
Compare: (NSString *)
Pass in a string that needs to be compared.
For example [value compare:@ "1234567890"], return nsorderedsame.
Options: (nsstringcompareoptions)
The value passed into the Nsstringcompareoptions enumeration
enum {
Nscaseinsensitivesearch = 1,// case insensitive comparison
Nsliteralsearch = 2,//Case-sensitive comparison
Nsbackwardssearch = 4,//Start Search from the end of the string
Nsanchoredsearch = 8,//search for a restricted range of strings
Nsnumbericsearch =//According to the numbers in the string, the order is calculated. such as Foo2.txt < Foo7.txt < Foo25.txt
The following definitions are higher than Mac OS 10.5 or more than iphone 2.0 is available
,
Nsdiacriticinsensitivesearch = +,//Ignore "-" symbol comparison
Nswidthinsensitivesearch = up,//Ignore length of string, compare results
Nsforcedorderingsearch =//ignores the option of case-insensitive comparisons and forces the return of nsorderedascending or nsordereddescending
The following definitions are more available than iphone 3.2
,
Nsregularexpressionsearch = 1024x768//can only be applied to rangeofstring: ..., stringbyreplacingoccurrencesofstring: ... and replaceoccurrencesofstring: ... Method. Using a generic compatible comparison method, if you set this, you can remove Nscaseinsensitivesearch and Nsanchoredsearch
}
Range: (Nsrange)
Comparing the range of strings
Structure variables:
Location: The starting position of the string to be compared (starting at 0)
Length: string lengths to compare
return value:
typedefEnum_nscomparisonresult {
nsorderedascending =-1, //
< Span style= "color: #008000;" > nsorderedsame, //= equals
nsordereddescending //
Span style= "color: #000000;" >} Nscomparisonresult;
Example: Version number comparison
NSString *num1 = @ "5.2.0";
nsstring*num2 = @ "5.3.0";
if ([num1compare:num2 options:nsnumericsearch] = = nsordereddescending)
{
Ulog (@ "%@ is bigger", NUM1);
}else
{
Ulog (@ "%@ is bigger", num2);
}
Explain:
Nsordereddescending is descending, and if NUMB1>NUMB2 is compared with this function then it is equal to descending.
IOS Learning Note 21:ios Compare string comparison