iOS客户端学习-字符串比较大小

来源:互联网 发布:同花顺软件官方网站 编辑:程序博客网 时间:2024/05/24 06:14

比较字符串是否相同isEqualToString

    NSString *str = [NSString stringWithFormat:@"This is %@","some body"];
 // 判断两个字符串是否相同,返回布尔值  
  BOOL isequal = [str isEqualToString:@"this is some body"];

字符串大小的比较compare

   字符串序列比列 compare,返回结果NSComparisonResult 
   type enum _NSComparisonResult{    
       NSOrderedAscending = -1,  
       NSOrderedSame,    
       NSOrderedDescending    
    }  

   int result = [@"abc" compare: @"bcd"];
   if(result ==NSOrderedAscending){
       // do some thing
   }

字符串大小的比较规则compareoptions

       compare 比较规则options
       NSLiteralSearch 区分大小写(完全比较)
       NSCaseInsensitiveSearch 不区分大小写
       NSNumericSearch 只比较字符串的个数,而不比较字符串的字面值

      int result = [@"This is John" compare:@"this is John" options:NSCaseInsensitiveSearch | NSNumericSearch]; 

比较字符串是否包含另一字符串hasPrefixhasSuffix,rangeOfString

      字符串开头是否包括另一字符串 hasPrefix,返回结果YES(true)
      字符串结尾是否包括另一字符串 hasSuffix,返回结果YES(true)
      查找字符串在另一字符串中的位置rangeOfString

      BOOL isHas = [str hasPrefix:@"This"];
      BOOL isHas = [str hasSuffix:@"This"];
      NSRange range = [str rangeOfString:@"is" options:NSCaseInsensitiveSearch];
      NSLog(@"The location in the string named 'str' of 'is' is @d",range.location); 
0 0