113,字符串查找

来源:互联网 发布:婴儿面膜淘宝授权书 编辑:程序博客网 时间:2024/06/06 12:50

#import <Foundation/Foundation.h>


int main(int argc,const char * argv[]) {

    //1,判断是否以什么开头

    NSString *str =@"http://www.baidu.com/img/lnj.gif";

    if ([str hasPrefix:@"http://"]) {

        NSLog(@"是一个URL");

    }else{

        NSLog(@"不是一个URL");

    }

    //2,判断是否以什么结尾

    if ([str hasSuffix:@".gif"]) {

        NSLog(@"是一张图片");

    }else{

        NSLog(@"不是一张图片!");

    }

    //3,判断字符串是否有包含什么

    /*

     只要str包含某字符串,那么,就会返回该字符串仔str中的起始位置location和长度length

     若没有包含,那么,location = NSNotFound,length = 0

     */

    NSRange range = [str rangeOfString:@"baidu"];

    if(range.location ==NSNotFound){

        NSLog(@"没找到");

    }else{

        NSLog(@"location = %lu,length = %lu",range.location,range.length);

    }

    return 0;

}

//2015-12-19 22:45:58.622 8,字符串查找[3327:247134]是一个URL

//2015-12-19 22:45:58.623 8,字符串查找[3327:247134]是一张图片

//2015-12-19 22:45:58.623 8,字符串查找[3327:247134] location = 11,length = 5

//Program ended with exit code: 0

0 0
原创粉丝点击