Comparing Strings With NSString In Objective-C

Many people, when they are first starting with Objective-C and iOS want to be able to compare strings in if-then statements and things like that. It seems natural to try to write something like:

NSString *string1 = @"A";
NSString *string2 = @"B";

if(string1 == string2)
     //do something
else
     //or not

NOTE: ^ the code above will not work…

But, that doesn’t work because strings are objects and if you want to compare strings you will need to use the NSString object methods that Apple has made available to you.

Testing For Equality With NSString

Most likely, you just want to see if your string is equal to another string. This is something that you can use in an if-then statement to direct the flow of your program. To do this you must use isEqualToString: function. This is something you send to the first string using the second string as a parameter. The result comes back as a BOOL (YES or NO) that you can evaluate.

See the example below:

NSString *myString1 = @"A";
NSString *myString2 = @"B";
NSString *myString3 = @"A";

BOOL isEqual = [myString1 isEqualToString:myString2];

if(isEqual)
     NSLog(@"%@ is equal to %@", myString1, myString2);
else
     NSLog(@"%@ is not equal to %@", myString1, myString2);

Looking For Prefixes and Suffixes

NSString also has two really convenient functions that you can use to find out if a string is at the beginning or end of your string. These two functions are called hasPrefix: and hasSuffix: respectively.

Check these out here:

NSString *name = @"Mr. John Smith, MD";

BOOL hasMrPrefix = [name hasPrefix:@"Mr"];

if(hasMrPrefix)
     NSLog(@"%@ has the Mr prefix", name);
else
     NSLog(@"%@ doesn't have the Mr prefix", name);

BOOL hasMDSuffix = [name hasSuffix:@"MD"];

if(hasMDSuffix)
     NSLog(@"%@ has the MD suffix", name);
else
     NSLog(@"%@ doesn't have the MD suffix", name);

Comparing Substrings With NSString

Finally, you can also compare substrings with the NSString function substringWithRange: . The first thing that you do is define an NSRange with the starting point and length of the string that you are interested in. Then you can use the first string's substringWithRange: function to extract the substring. Once you do this you can use the same comparison methods as we just discussed.

Here is an example of that in action:

NSString *alphabet = @"ABCDEFGHIJKLMONPQRSTUVWXYZ";

NSRange range = NSMakeRange(2, 3);

BOOL lettersInRange = [[alphabet substringWithRange:range] isEqualToString:@"CDE"];

if(lettersInRange)
     NSLog(@"The letters CDE are in alphabet starting at position 2");
else
     NSLog(@"The letters CDE aren't in alphabet starting at position 2");

So, that's it for comparing strings with NSString in Objective-C. Any thoughts or other ideas to help people to work with strings?

3 Responses to Comparing Strings With NSString In Objective-C

  1. tech_niek January 25, 2012 at 8:27 am #

    Hi Mark,

    First of all, I am learning a lot from your blog. Thank you providing us newbees with your great tips.
    In your example I guess you’re missing BOOL hasMrSuffix = [name hasSuffix:@"Mr"];
    as the first line in your prefix/suffix example.

    Thanks,
    tech_niek

    • MattjDrake January 25, 2012 at 2:04 pm #

      @tech_niek thanks for that and for the feedback too. Just fixed the blog entry to include the suffix test.

  2. yosan February 10, 2012 at 9:31 am #

    Hi Mark,

    Thanks for this tutorial. But I have a question, what if the string that I’m looking for is in the middle? Like this :

    NSString *sourceString = @”I am a junior programmer”;

    if(sourceString contains @”junior” is true){
    //do something
    }

    Thanks you

Leave a Reply

Switch to our mobile site