牛骨文教育服务平台(让学习变的简单)
博文笔记

iOS 判断字符串是否含有某种字符

创建时间:2015-05-30 投稿人: 浏览次数:1106

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

打印结果:

声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。