Transferred from: http://blog.sina.com.cn/s/blog_897dd7be0100teh6.html
For a few months, many of the methods of thinking can follow certain rules. 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:
typedef enum _nscomparisonresult {
nsorderedascending = - 1 , // < ascending
nsorderedsame, // = equals
nsordereddescending // > Descending
} Nscomparisonresult;