Home Tutorial Iphone Examples iPhone Compare String

 
 

Share on Google+Share on Google+
iPhone Compare String
Posted on: September 2, 2009 at 12:00 AM
Advertisement
In this tutorial will learn how to compare two string in iPhone using Compare, when button is clicked. This application is window Based Application, will set action for the button and whenever button is clicked will show result. my project name is compareString.

Compare String

Final application will look like:  



In this tutorial will learn how to compare two string in iPhone using Comparestring(), when button is clicked. This application is window Based Application, will set action for the button and whenever button is clicked will show result. my project name is compareString().

Add this in .h file:

 

IBOutlet UIButton *show;

    NSString *string;

@property (nonatomic, retain) IBOutlet    NSString *string;

@property (nonatomic, retain) IBOutlet    UIButton *show;

-(IBAction)compareString:(id)sender;

 Add this in .m file:

 

@synthesize window, string, show;

 -(IBAction)compareString:(id)sender

 {

    NSString *string1 = @"Rose";  // assigning value to string

    NSString *string2 = @"India"; // assigning value to string

    int val=[string1 compare: string2]; // in it we declare it as integer type

     NSString *s=[[NSString alloc] initWithFormat:@"Value is: %d",val];   

     // here we alloc it and is printed by using NSLog.

    NSLog(s);

 }

make connection of button in Interface Builder as below.


 

 
 
In this we are writing the method for action here we are taking two string and comparing, 
1 indicates strings are not equal and 0 indicates strings are equal.

Download the Source Code

 

Advertisement

Related Tags for iPhone Compare String:


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: September 2, 2009

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com