iOS 判断NSString是否包含某个字符串

主要是使用3个方法

  1. rangeOfString    是否包含
  2. hasPrefix        是否在前缀包含
  3. hasSuffix           是否在末尾包含

如代码:

    //判断字符是否包含某字符串;
    NSString *staA = @"hello,JiNan,jkk";
    NSRange theRange =[@"jkk" rangeOfString:staA];
    NSLog(@"%d %d",theRange.location,theRange.length);
    NSRange range2 = [staA rangeOfString:@"jkk"];
    NSLog(@"%d %d",range2.location,range2.length);
    
    //字条串开始包含有某字符串
    if ([staA hasPrefix:@"hello"]) {
        NSLog(@"has hello");
    }
    
    //字符串末尾有某字符串;
    if ([staA hasSuffix:@"jkk"]) {
        NSLog(@"has jkk");
    }

打印结果:

原文地址:https://www.cnblogs.com/cocoajin/p/3480645.html