IOS Learning Note 21:ios Compare string comparison

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.