Frage

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.

War es hilfreich?

Lösung

Simply use -[NSString hasSuffix:].

Andere Tipps

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!");
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top