Wednesday, 9 May 2012

String Comparison in Objective C




 In Objective C, strings can be compared using 'isEqualToString'.
 
NSString *myString1 = @”Suraj”;
NSString *myString2 = @”Suraj”;
NSString *myString3 = @”Mirajkar”;

if ([myString1 isEqualToString: myString2])
 {
          NSLog(@”String1: %@ is Equal to String2: %@”, myString1, myString2); 
 }
 else
 {
            NSLog(@”String1: %@ is Not Equal to String2: %@”, myString1, myString2);
 }

 if ([myString1 isEqualToString: myString3])
 {
            NSLog(@”String1: %@ is Equal to String3: %@”, myString1, myString3);
 }
 else
 {
           NSLog(@”String1: %@ is Not Equal to String3: %@”, myString1, myString3);
 }

                   // Output :
                   // String1: Suraj is Equal to String2: Suraj
                   // String1: Suraj is Not Equal to String3: Mirajkar

 For Case Insensitive String Comparison:
 
NSString *myString1 = @”Suraj”;
NSString *myString2 = @”suraj”;

if ([myString1 caseInsensitiveCompare: myString2] == NSOrderedSame)   
 {
          NSLog(@”String1: %@ is Equal to String2: %@”, myString1, myString2); 
 }
 else
 {
            NSLog(@”String1: %@ is Not Equal to String2: %@”, myString1, myString2);
 }

                    // Output :
                   // String1: Suraj is Equal to String2: suraj

1 comment:

  1. Dick and Lenay . I am a cancer survivor and a domestic abuse survivor. I have three sons, two of whom have juvenile diabetes. I have worked in the healthcare industry in some capacity for the past 30 years and as an entrepreneur. I am now combining my passion for both fields in this blog with a goal to provide helpful information on weight loss. My bigger goal is to help fund a cure for juvenile diabetes. - See more at: About Us
    Natural Weight Loss
    Lose Weight
    Weight Loss
    Diet
    Weight Management

    ReplyDelete