黑马程序员技术交流社区

标题: NSString检索字符串前后缀 [打印本页]

作者: neverbloom7    时间: 2015-8-27 21:11
标题: NSString检索字符串前后缀
NSString检索字符串前缀
NSString *url=@“http://www.baidu.com”;
if ([url hasPrefix:@“http://”] || [url hasPrefix:@“https://”]){
NSLog(@“这是一个网址”);
}else{
NSLog(@“这不是一个网址”);
}

NSString检索字符串后缀
NSString *imgName=@“xxxx.jpg”;
if([imgName hasSuffix:@“.jpg”] || [imgName hasSuffix:@“.png”]  || [imgName hasSuffix:@“.gif”]  ||  [imgName hasSuffix:@“.jpeg”]){
NSLog(@“这是一个图片”);
}else{
NSLog(@“这不是一个图片”);
}
作者: neverbloom7    时间: 2015-8-27 21:13
NSString中,某字符串首次出现的检索
1 rangeOfString  2//在1中查找首次出现的2
NSString *str1=@“”;
NSString *str2=@“”;

//NSRange是自定义的结构体,有两个成员location,length,
1.如果查找到了,返回查找字符串在原字符串的位置和长度,
2.如果没找到,返回特别大的一个数,最大的long型的数(location),和0(length)
NSRange range=[str1 rangeOfString str2];
if(range.location!=NSNotFound){
NSLog(@“%lu,%lu”,range.location,range.length);
}else{
NSLog(@“没有检测到%lu”,str2);
}

url hasPrefix:@“http://”
imgName hasSuffix:@“.jpg”  
返回值是BOOL类型的,用作在if语句中判断YES或者NO

NSNotFound  是否找到




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2