Question

I have a string *strSearchNumber = @"9970405060" and want to check whether it is present in string *strPhoneMobile = @"+91997040560"

How can I check if last 6 or 8 characters of my string are present in some other string. Thanks in advance.

Était-ce utile?

La solution

Simply use -[NSString hasSuffix:].

Autres conseils

Use the NSString method hasSuffix:

NSString *strSearchNumber = @"9970405060";
NSString *strPhoneMobile  = @"+91997040560";
if ([strPhoneMobile hasSuffix: strSearchNumber]) {
    // do something
}

How do I check if a string contains another string in Objective-C?

Better then just checking the end or front of the string. You should check if the string contains the other bit of string. The link above will give you the answer.

Underneath the short version of the answer.

NSString *string = @"hello bla bla";
if ([string rangeOfString:@"bla"].location == NSNotFound) {
  NSLog(@"string does not contain bla");
} else {
  NSLog(@"string contains bla!");
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top