Share on Google+Share on Google+

iPhone SDK Comparing Strings

Learn how to comparing strings in the iPhone sdk project code.

In this section I will show you how you can compare the two strings in your iPhone SDK Project.

To compare the Strings you will have to use the isEqualToString function to compare the strings.
You can simple compare the strings with == operator. In the following example code I have compared the two strings:

        NSString *str1 = @"Apple";;

        NSString *str2 = @"Orange";

        if([str1 isEqualToString: str2]){

          UIAlertView* alert = [[UIAlertView alloc] initWithTitle:nil message:@"Both strings are equal." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];

          [alert show];

          [alert release];  


          UIAlertView* alert = [[UIAlertView alloc] initWithTitle:nil message:@"Strings are not equal" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];

          [alert show];

          [alert release];


The above example is the proper way of comparing strings in iPhone sdk programs. The == operator only compares the pointers, but instead we are comparing the the actual strings.

 Hope the above example code will help you in quickly compare the strings.



Posted on: August 22, 2009 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.


Advertisement null