compare string from variable in objective-c
I have tried all possible things to compare strings in Objective-C.
compare: isEqualToString:
It works perfect when I do
if ([string1 compare:@"sometext"])
{
//dostuff
}
But if I am to use a variable like string2 it fails all the time.
if ([string1 compare:string2])
I also tried
if ([string1 compare:(@"%@", string2)])
but it doesn't seem to work.
Content of the both strings are md5 checksums like:
NSString *string1 = "eb121296f0ed90be93578e50bedb27e3"
NSString *string2 = "eb121296f0ed90be93578e50bed开发者_StackOverflowb27e3"
I tried all these methods with isEqualToString: as well, without any results...
Is it me or is obj-c driving me crazy? Thanks in advance, Wouter
Sorry, maybe I'm misreading, but are you trying to get the order of the strings, or just see if they are the same string. If you're just testing for equality try this.
Edited after the comments
Since you are creating the string from an NSData type try this.
NSString *string2 = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
if ([string1 isEqualToString:string2]) {
// do stuff
}
you need to write
if ([string1 compare:string2] == NSOrderedSame )
精彩评论