Home Tutorial Iphone Examples iPhone SDK Comparing Strings

 
 

Share on Google+Share on Google+
iPhone SDK Comparing Strings
Posted on: August 22, 2009 at 12:00 AM
Advertisement
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.
 

 

Advertisement

Related Tags for iPhone SDK Comparing Strings:


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.

Posted on: August 22, 2009

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com