How to best compare two NSString objects while ignoring case? - objective-c

I want to compare two strings. It fails when the string have capital letter. How do I convert both string to capitalize and compare.
I have a sample code, can someone correct this.
if ([[txtAnswer.text capitalizedString] isEqualToString:[answer capitalizedString]]) {
// Do somehing
}

If you look at the NSString class reference you will see under the heading Identifying and Comparing Strings the methods caseInsensitiveCompare: and localizedCaseInsensitiveCompare:.

You might try something like:
if ([txtAnswer.text caseInsensitiveCompare: answer] == NSOrderedSame) {
// do something.
}

You can do a case insensitive string compare.
if([txtAnswer.text compare:answer options:NSCaseInsensitiveSearch] == NSOrderedSame)
{
// Do somehing
}

Related

if NSString does not equal function?

I have searched all over the place for this, including apple's docs on NSString (maybe I didn't see?) but I am trying to find a method in xCode for checking wether a NSString does not equal to something. Much like
if (myNSSting = #"text" {...
except specifically I want to check if it does not equal to 'text'.
if(![myNSString isEqualToString:#"text"])
For case insensitive compare we can handle by:
if( [#"Some String" caseInsensitiveCompare:#"some string"] == NSOrderedSame ) {
// strings are equal except for possibly case
}

Using a function argument inside a function in Objective-C

I am new to Objective-C development and I have already run into the first problem. I am trying to write a function that takes a string as argument and compares that string with other strings. The pseudo code for the function would be the following:
String string2
String string3
function compare_string (String string) {
if (string == "a specific string" &&
string == string2 &&
string == string3) {
// do something
}
}
Until now I only have the following:
NSString *string2 = #"Second string";
NSString *string3 = #"Third string";
(void) compare_string: (NSString *) string {
but now I am already stuck because I don't know how to call the input string string inside the function. Should I simply do something like
if (string == string2) { ... }
or is there another way to use function arguments in Objective-C?
This will be like this:
-(void)compareString:(NSString *string){
if ( [string isEqualToString:#"a specific string"] && [string isEqualToString:string2] && [string isEqualToString:string3] ){
// do something
}
}
NOTE: str1==str2 will compare the memory address, and isEqualToString: compares the contents of the strings.
Check out the docs for NSString there are a bunch of methods that do this for you already - so you don't need to make your own string comparison code
An example would look like
if ([string compare:string2] == NSOrderedSame) { ... }
This is handy if you wish to do complex comparisons like case insensitive etc
for simple equality you could just use
if ([string isEqualToString:string2]) { ... }
Edit
I mis read the question - here is an answer that actually relates to what you asked
You need to use the isEqual: methods defined on object for checking equally. == will only check the pointer values.
You could write your method like this
- (void)compareString:(NSString *)string;
{
if ([#[string, string, string] isEqual:#[ #"a specific string", string1, string2]]) {
// do stuff
}
}
Then later calling it would look like this
[self compareString:#"A string to compare"];
NB
Wrapping it up in arrays reads slightly nicer than comparing them all individually

Parsing json incorrect

i'm parsing something from the Apple JSON (the rating of the app) and i tried something like:
if ([StoreParse objectForKey:#"averageUserRating"] == #"4.5") {
NSLog(#"xx");
} else {
NSLog(#"xxx");
}
The App has a rating of 4.5 and if i do
NSlog (#"%#", [StoreParse objectForKey:#"averageUserRating"]);
The output is : 4.5
but when i run the script the NSlog in the first code's output is "xxx" does anyone can help me?
Comparing strings, which are essentially pointers to instances of the NSSring class, is erroneous, as two identical-content strings can have a different memory address. Use
if ([[StoreParse objectForKey:#"averageUserRating"] isEqualToString:#"4.5"])
instead.
Use isEqualToString:
if ([[StoreParse objectForKey:#"averageUserRating"] isEqualToString:#"4.5"]) {
NSLog(#"xx");
}
You can't do this:
if ([StoreParse objectForKey:#"averageUserRating"] == #"4.5")
You need to do:
if ([[StoreParse objectForKey:#"averageUserRating"] isEqualToString:#"4.5"])
That's assuming it's a string. If it's an NSNumber then do:
if ([[StoreParse objectForKey:#"averageUserRating"] floatValue] == 4.5f)
(Although be careful about comparing equality of floats)
See this question for more information on string equality.
If the value for averageUserRating is an NSNumber, you should convert it to a formatted NSString first then compare it to the #"4.5" string literal:
if ([[NSString stringWithFormat:#"%1.1f", [[StoreParse objectForKey:#"averageUserRating"] floatValue]] isEqualToString:#"4.5"])

Objective-C string comparison

I have an array of names but can't seem to make the comparison work. Do I have an improper use of the language here?
NSLog(#"%#",[arrayOfNames objectAtIndex:0]);
if ([arrayOfNames objectAtIndex:0] == "Blue"){
NSLog(#"it's Blue");
}
else {
NSLog(#"it's not Blue");
}
The output is the following one:
Blue
it's not Blue
Use the following:
if ([[arrayOfNames objectAtIndex:0] isEqualToString:#"Blue"])
You're comparing two objects (one of the id-type, the other is a C-string) with the == operator. The comparison will fail, since they are 2 different objects. With the isEqualToString you are comparing the value of the object to the string #"Blue".

String compare Objective-C

I've been struggling with a simple comparison but I can't get it to work.
I´m reading a XML file and I need to compare data from it in order to show the right picture.
http://www.cleaner.se/larm.xml (Example file for parsing)
I have tried things like:
if([aLarm.larmClass isEqualToString:#"A"])
NSLog(#"same");
else
NSLog(#"Not same");
If I use: NSLog(aLarm.larmClass); console puts it out nicely as it should. What am I doing wrong?
You can use the NSString compare: methods. For example:
if ([myString caseInsensitiveCompare:#"A"] == NSOrderedSame ) {
NSLog(#"The same");
} else {
NSLog(#"Not the same.");
}
The result is an NSComparisonResult which is just an enum with types NSOrderedSame, NSOrderedAscending and NSOrderedDescending.
Check the documentation on the various compare: methods here.
Of course, if the receiver is actually an NSString, then isEqualToString: should also work. So if you're trying to compare a class name (aLarm.larmClass ??), then you can call:
if ([NSStringFromClass([aLarm class]) isEqualToString:#"A"] ) {
NSLog(#"The same");
}
If the larmClass property is a string, make sure that it is actually one character in length (i.e. it doesn't have any leading or trailing whitespace that was accidentally included when parsing the XML). If the larmClass property truly is an NSString containing the letter ‘A’ then [aLarm.larmClass isEqualToString:#"A"] will return YES.
Do a:
NSLog(#"%u, %#", [aLarm.larmClass length], aLarm.larmClass);
and just make sure that it shows “1, A”.