Case-insensitive NSString comparison

前端 未结 4 1522
梦毁少年i
梦毁少年i 2021-02-05 19:35

Using this code I am able to compare string values.

[elementName isEqualToString: @\"Response\"]

But this compares case-sensitively. Is there a

4条回答
  •  一生所求
    2021-02-05 19:53

    NSString *string1 = @"stringABC";
    NSString *string2 = @"STRINGDEF";
    NSComparisonResult result = [string1 caseInsensitiveCompare:string2];
    
    if (result == NSOrderedAscending) {
      NSLog(@"string1 comes before string2");
    } else if (result == NSOrderedSame) {
      NSLog(@"We're comparing the same string");
    } else if (result == NSOrderedDescending) {
       NSLog(@"string2 comes before string1");
    }
    

提交回复
热议问题