Reputation: 245
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?
Upvotes: 15
Views: 22772
Reputation: 1972
The method below will return Yes if the given string contains the given char
-(BOOL)doesString:(NSString *)string containCharacter:(char)character
{
return [string rangeOfString:[NSString stringWithFormat:@"%c",character]].location != NSNotFound;
}
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
Upvotes: 16
Reputation: 853
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!!");
Upvotes: 2
Reputation: 882
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];
}
Upvotes: 2
Reputation: 135
- (bool) contains: (NSString*) substring {
NSRange range = [self rangeOfString:substring];
return range.location != NSNotFound;
}
Upvotes: 2
Reputation: 127
You can use
NSString rangeOfString: (NSString *) string
If the NSRange comes back with property 'location' equal to NSNotFound, then the string does not contain the passed string (or character).
Upvotes: 0
Reputation: 185661
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
}
Upvotes: 38