How to comapare the string character by character in Objective c?
Iam developing one applciation.In that i want to开发者_运维百科 comapare the every character of string with other character.So please tell me how to do that one.
Use characterAtIndex:
function of NSString
to extract the character by index.
- (unichar)characterAtIndex:(NSUInteger)index
for (int i=0; i<[string length]; i++) {
char = [string characterAtIndex:i];
NSString *charString = [NSString stringWithFormat:@"%c", char];
if ([charString isEqualToString:comparisonString]) {
//match
}
else {
//no match
}
}
if you want to compare a string. Using isEqualToString method.
NSMutableString *str = [[NSMutableString alloc] initWithString:@"a"];
if([str isEqualToString:@"a"]){
// match
}
else{
// not match
}
精彩评论