iPhone SDK Comparing Strings


 

iPhone SDK Comparing Strings

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

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];  

      }else{

          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.
 

 

Ads