Jonathan
Jonathan

Reputation: 167

Comparing Strings in Objective-C

I'm writing a small app for the iphone and I'm trying to write a function that will insert an NSMutableString into an NSArray in alphabetical order. Also I'll be writing a sort to sort the entire array as well. For both cases I'm wondering what the best way of comparing NSMutableStrings is. Is there a specific function I can use?

Thanks for your help.

Upvotes: 0

Views: 884

Answers (6)

Hector
Hector

Reputation: 3907

For sorting array

NSArray *sortedArray = [anArray sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];

Upvotes: 2

Parth Bhatt
Parth Bhatt

Reputation: 19469

I think this should work for you. This is my answer which I have taken from the link:

Comparing text in UITextView?

SOLUTION-1: I have modified it here a bit to make it more easier for your case:

Let us assume String1 is one NSString.

 //Though this is a case sensitive comparison of string
 BOOL boolVal = [String1 isEqualToString:@"My Default Text"];

 //Here is how you can do case insensitive comparison of string:
 NSComparisonResult boolVal = [String1 compare:@"My Default Text" options:NSCaseInsensitiveSearch]; 

 if(boolVal == NSOrderedSame)
 {
     NSLog(@"Strings are same");
 }
 else
 {
     NSLog(@"Strings are Different");
 }

Here if boolVal is NSOrderedSame then you can say that strings are same else they are different.

SOLUTION-2: Also you don't find this easy, you can refer to Macmade's answer under the same link.

Hope this helps you.

Upvotes: 2

Chuck
Chuck

Reputation: 237010

If you look under "Identifying and comparing strings" in the NSString reference, you'll find several options. They do slightly different things, since you might want to compare strings in different ways (e.g. are numbers compared in lexical or numeric order?). The most basic is compare: — you can probably start there and choose a more complicated version as needed.

Upvotes: 2

ThomasW
ThomasW

Reputation: 17317

I think you're looking for

(NSComparisonResult)[aString compare: bString];

https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/nsstring_Class/Reference/NSString.html#//apple_ref/occ/instm/NSString/compare:

You can use this or one of the related methods if you're doing insertion sort. However, if you want to do a one time sort of the NSMutableArray, you can use one of the NSMutableArray sorting methods such as sortUsingComparator:.

https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSMutableArray_Class/Reference/Reference.html#//apple_ref/occ/instm/NSMutableArray/sortUsingComparator:

Upvotes: 4

Kjuly
Kjuly

Reputation: 35131

Try NSArray's sortedArrayUsingSelector: method:

NSArray * stringArray = [NSArray arrayWithObjects:@"dddsss", @"aada", @"bbb", nil];
[stringArray sortedArrayUsingSelector:@selector(compare:)];
NSLog(@"%@", [stringArray sortedArrayUsingSelector:@selector(compare:)]);

Out put:

(
  aada,
  bbb,
  dddsss
)

What's more, you can use NSSortDescriptor to decide ASC or DESC order.

Upvotes: 1

Raptor
Raptor

Reputation: 54212

try BOOL ans = [str1 isEqualToString:str2];

Upvotes: 1

Related Questions