Naresh Venkat
Naresh Venkat

Reputation: 319

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.

Upvotes: 0

Views: 113

Answers (3)

Mundi
Mundi

Reputation: 80273

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
   }
}

Upvotes: 2

Jhaliya - Praveen Sharma
Jhaliya - Praveen Sharma

Reputation: 31722

Use characterAtIndex: function of NSString to extract the character by index.

- (unichar)characterAtIndex:(NSUInteger)index

Upvotes: 3

Chau Than
Chau Than

Reputation: 150

if you want to compare a string. Using isEqualToString method.

NSMutableString *str = [[NSMutableString alloc] initWithString:@"a"];
if([str isEqualToString:@"a"]){
  // match
}
else{
  // not match
}

Upvotes: 0

Related Questions