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");
    }

打印結果:


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM