Search code examples
objective-cnsstringprefix

NSString HasPrefix going wrong


i've problem with nsstring hasprefix

for example. i've string like this

NSString *data= @"NZDIDR,NZDIDR,9571,9593,9571,IDR,2013-09-23 21:45:25.0,CUR",

if([clean hasPrefix:@"CUR"]||[clean hasPrefix:@"cur"]){
     NSLog(@"yep! there is CUR");
}
else{
   NSLog(@"no there is!");
}

the result should be"yep! there is CUR" right? is that error on my method?


Solution

  • What you mean to do is check for a suffix. A prefix is at the start of the string, a suffix is at the end.

    Also, your code checks for a prefix on the object clean not data.

    Try:

    if ([data hasSuffix:@"CUR"]) {
      NSLog(@"Suffix found.");
    }