iPhone Compare String


 

iPhone Compare String

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.

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

 

Ads