質問

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.

役に立ちましたか?

解決

Simply use -[NSString hasSuffix:].

他のヒント

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!");
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top