主要是使用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");
    }

打印结果:

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

相关文章:

  • 2022-01-07
  • 2022-12-23
猜你喜欢
  • 2021-07-25
  • 2022-12-23
  • 2022-12-23
  • 2022-01-07
  • 2022-12-23
  • 2022-12-23
  • 2022-02-28
相关资源
相似解决方案