NSString * str = @"https://blog.csdn.net";
NSRange range = [str rangeOfString:@"http"];
NSLog(@"%@", NSStringFromRange(range));
if(range.length == 0){
NSLog(@"str中没找到目标字符串 ‘http’ ");
}
if(range.location == NSNotFound){
NSLog(@"str中没找到目标字符串 ‘http’");
}else if(range.location==0){
NSLog(@"str中找到目标字符串‘http’并且在头部");
}else{
NSLog(@"str中找到目标字符串‘http’");
}
-
使用NSString方法中的 -(BOOL)containsString:(NSString *)str;
NSString * str = @"https://blog.csdn.net";
if([str containsString:@"http"]){
NSLog(@"str 包含 ‘http’");
}else{
NSLog(@"str 不包含 ‘http’");
}
使用NSString方法中的 -(BOOL)hasPrefix:(NSString *)str;-(BOOL)hasPrefix:(NSString *)str;
NSString * str = @"https://blog.csdn.net";
if([str hasPrefix:@"http"]){
NSLog(@"str 是 ‘http’ 开头的");
}else{
NSLog(@"str 不是 ‘http’ 开头的");
}
if([str hasPrefix:@"net"]){
NSLog(@"str 是 ‘net’ 结尾");
}else{
NSLog(@"str 不是 ‘net’ 结尾");
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)