How to detect if NSString contains a particular character or not?

匿名 (未验证) 提交于 2019-12-03 03:04:01

问题:

i have one NSString object , for ex:- ($45,0000)

Now i want to find if this string contains () or not

How can i do this?

回答1:

Are you trying to find if it contains at least one of ( or )? You can use -rangeOfCharacterFromSet::

NSCharacterSet *cset = [NSCharacterSet characterSetWithCharactersInString:@"()"]; NSRange range = [mystr rangeOfCharacterFromSet:cset]; if (range.location == NSNotFound) {     // no ( or ) in the string } else {     // ( or ) are present } 


回答2:

The method below will return Yes if the given string contains the given char

-(BOOL)doesString:(NSString *)string containCharacter:(char)character {     if ([string rangeOfString:[NSString stringWithFormat:@"%c",character]].location != NSNotFound)     {         return YES;     }     return NO; } 

You can use it as follows:

 NSString *s = @"abcdefg";      if ([self doesString:s containCharacter:'a'])         NSLog(@"'a' found");     else         NSLog(@"No 'a' found");       if ([self doesString:s containCharacter:'h'])         NSLog(@"'h' found");     else         NSLog(@"No 'h' found"); 

Output:

2013-01-11 11:15:03.830 CharFinder[17539:c07] 'a' found

2013-01-11 11:15:03.831 CharFinder[17539:c07] No 'h' found



回答3:

- (bool) contains: (NSString*) substring {     NSRange range = [self rangeOfString:substring];     return range.location != NSNotFound; } 


回答4:

I got a generalized answer to your question which I was using in my code. This code includes the following rules: 1. No special characters 2. At least one capital and one small English alphabet 3. At least one numeric digit

 BOOL lowerCaseLetter,upperCaseLetter,digit,specialCharacter;     int asciiValue;     if([txtPassword.text length] >= 5)     {         for (int i = 0; i < [txtPassword.text length]; i++)         {             unichar c = [txtPassword.text characterAtIndex:i];             if(!lowerCaseLetter)             {                 lowerCaseLetter = [[NSCharacterSet lowercaseLetterCharacterSet] characterIsMember:c];             }             if(!upperCaseLetter)             {                 upperCaseLetter = [[NSCharacterSet uppercaseLetterCharacterSet] characterIsMember:c];             }             if(!digit)             {                 digit = [[NSCharacterSet decimalDigitCharacterSet] characterIsMember:c];             }              asciiValue = [txtPassword.text characterAtIndex:i];             NSLog(@"ascii value---%d",asciiValue);             if((asciiValue >=33&&asciiValue < 47)||(asciiValue>=58 && asciiValue<=64)||(asciiValue>=91 && asciiValue<=96)||(asciiValue>=91 && asciiValue<=96))             {                 specialCharacter=1;             }             else             {                 specialCharacter=0;             }          }          if(specialCharacter==0 && digit && lowerCaseLetter && upperCaseLetter)         {             //do what u want             NSLog(@"Valid Password %d",specialCharacter);         }         else         {             NSLog(@"Invalid Password %d",specialCharacter);             UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error"                                                             message:@"Please Ensure that you have at least one lower case letter, one upper case letter, one digit and No Any special character"                                                            delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];             [alert show];         } 


回答5:

Why to always use for NSCharactorSet ?? this is simple and powerful solution

NSString *textStr = @"This is String Containing / Character";  if ([textStr containsString:@"/"]) {     NSLog(@"Found!!"); } else {    NSLog(@"Not Found!!"); 


回答6:

You can use

NSString rangeOfString: (NSString *) string 

See here: https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html%23//apple_ref/occ/instm/NSString/rangeOfString:

If the NSRange comes back with property 'location' equal to NSNotFound, then the string does not contain the passed string (or character).



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!