Nspredicate Query/Search

Source: Internet
Author: User

Description: The nspredicate in the cocoa Framework is used for querying, both in principle and in usage similar to where in SQL, which acts as a filter for the database.

The most commonly used function

+ (Nspredicate *) Predicatewithformat: (NSString *) Predicateformat, ...;

1. Compare operators >, <, = =, >=, <=,! =
Example: @ "number >= 99"

2. Scope operator: in, between
Example: @ "number between {1,5}"
@ "address in {' Shanghai ', ' Nanjing '}"

3. String itself: Self
Example: @ "self = = ' APPLE '"

4. String-Related: Beginswith, ENDSWITH, CONTAINS
Example: @ "name CONTAIN[CD] ' ang '"//contains a string
@ "name Beginswith[c] ' sh '"//start with a string
@ "name Endswith[d] ' ang '"//End with a string
Note: [C] is not case sensitive, [d] does not differentiate between pronounced symbols without accents, [CD] is neither case-sensitive, nor does it distinguish between pronounced symbols.

5. Wildcard characters: Like
Example: @ "name LIKE[CD] ' *er* '"//* represents a wildcard, like also accepts [CD].
@ "name LIKE[CD] '??? Er* ' "

6. Regular expression: MATCHES
Example: NSString *regex = @ "^a.+e$"; Start with a, E end
@ "Name MATCHES%@", regex

Practical application: Filter the Nsarray

Nsarray *array = [[Nsarray alloc]initwithobjects:@ "Beijing", @ "Shanghai", @ "Guangzou", @ "Wuhan", Nil];  NSString *string = @ "Ang";  Nspredicate *pred = [Nspredicate predicatewithformat:@ "Self CONTAINS%@", string];  NSLog (@ "%@", [array filteredarrayusingpredicate:pred]);  

Practical application: Determine if the first letter of the string is a letter

NSString *regex = @ "[a-za-z]+";  Nspredicate *predicate = [Nspredicate predicatewithformat:@ "Self MATCHES%@", regex];    if ([predicate evaluatewithobject:astring]) {  }  

Practical application: String substitution

nserror* error = NULL;  nsregularexpression* regex = [Nsregularexpression regularexpressionwithpattern:@ "(encoding=\") [^\ "]+ (\") "                                                                              options:0                                                                              error:&error];  nsstring* sample = @ "<xml encoding=\" abc\ "></xml><xml encoding=\" def\ "></xml><xml Encoding=\ "ttt\" ></xml> ";  NSLog (@ "start:%@", sample);  nsstring* result = [regex stringbyreplacingmatchesinstring:sample                                                        options:0                                                         range:nsmakerange (0, sample.length)                                                        withtemplate:@ "$1utf-8$2"];  NSLog (@ "result:%@", Result);  

Practical application: Intercepting strings

Assemble a string, you need to parse the URL inside  nsstring *[email protected] "<meta/><link/><title>1q84 book1</ Title>

Practical application: Judging mobile phone number, phone number function

Regular Judge mobile phone number address format-(BOOL) Ismobilenumber: (NSString *) mobilenum{/** * Mobile phone number * Move: 134[0-8],135,136,137,138,        139,150,151,157,158,159,182,187,188 * Unicom: 130,131,132,152,155,156,185,186 * Telecom: 133,1349,153,180,189 * *       NSString * MOBILE = @ "^1 (3[0-9]|5[0-35-9]|8[025-9]) \\d{8}$";         /** 10 * Mobile: China Mobile 11 * 134[0-8],135,136,137,138,139,150,151,157,158,159,182,187,188 */NSString * CM = @ "^1 (34[0-8]| (       3[5-9]|5[017-9]|8[278] \\d) \\d{7}$ "; /** 15 * Unicom: China Unicom * 130,131,132,152,155,156,185,186 * * N       sstring * CU = @ "^1 (3[0-2]|5[256]|8[56]) \\d{8}$"; /** 20 * China Telecom: Chinese Telecom * 133,1349,153,180,189 * * * NSString *       CT = @ "^1 ((33|53|8[09]) [0-9]|349) \\d{7}$";   /** 25 * Mainland China fixed and PHS 26 * Area code: 010,020,021,022,023,024,025,027,028,029 27      * Number: seven-bit or eight-bit * *//NSString * PHS = @ "^0 (10|2[0-5789]|\\d{3}) \\d{7,8}$";     Nspredicate *regextestmobile = [Nspredicate predicatewithformat:@ "Self MATCHES%@", MOBILE];     Nspredicate *REGEXTESTCM = [Nspredicate predicatewithformat:@ "Self MATCHES%@", CM];     Nspredicate *REGEXTESTCU = [Nspredicate predicatewithformat:@ "Self MATCHES%@", CU];        Nspredicate *REGEXTESTCT = [Nspredicate predicatewithformat:@ "Self MATCHES%@", CT]; if ([regextestmobile evaluatewithobject:mobilenum] = = YES) | | ([regextestcm evaluatewithobject:mobilenum] = = YES) | | ([regextestct evaluatewithobject:mobilenum] = = YES) | |    ([regextestcu evaluatewithobject:mobilenum] = = YES))        {if ([regextestcm evaluatewithobject:mobilenum] = = YES) {NSLog (@ "China Mobile");        } else if ([regextestct evaluatewithobject:mobilenum] = = YES) {NSLog (@ "China Telecom"); } else if ([regextestcu evaluatewithobject:mobilenum] = = YES) {NSLog(@ "China Unicom");        } else {NSLog (@ "Unknow");    } return YES;    } else {return NO; }}

Practical application: Mailbox verification, phone number verification

is a valid regular expression + (BOOL) isvalidateregularexpression: (NSString *) strdestination byexpression: (NSString *) strexpression {   Nspredicate *predicate = [nspredicatepredicatewithformat:@ "Self MATCHES%@", strexpression];     return [predicate evaluatewithobject:strdestination];} Verify email+ (BOOL) Isvalidateemail: (NSString *) Email {   NSString *strregex = @ "[A-z0-9a-z._%+-][email protected][ A-za-z0-9.-]+\\. [A-za-z] {1,5} ";   BOOL RT = [Commontools isvalidateregularexpression:email Byexpression:strregex];   return RT;} Verify Phone + (BOOL) Isvalidatetelnumber: (NSString *) Number {   NSString *strregex = @ "[0-9]{1,20}";   BOOL RT = [Commontools isvalidateregularexpression:number Byexpression:strregex];   return RT;}

Practical application: NSDate to filter

Date within 10 days: nsdate *enddate = [[NSDate date] retain]; Nstimeinterval timeinterval= [endDate timeintervalsincereferencedate];timeinterval-=3600*24*10; NSDate *begindate = [[NSDate datewithtimeintervalsincereferencedate:timeinterval] retain];//filters the CoreData ( Suppose there are fetchrequest) nspredicate *predicate_date =[nspredicate predicatewithformat:@ "date >=%@ and date <=%@", Begindate,enddate];    [fetchrequest setpredicate:predicate_date];//Release retained Object [EndDate release]; [Begindate release];

Nspredicate Query/Search

Related Article

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.