如何在字符串 url 中搜索字符串值?
字符串网址:
http://localHost:8070/serviceCase=ActiveService
http://localHost:8070/serviceCase=KoplService
字符串值为:
ActiveService
我需要打印具有“ActiveService”的网址!
如何在字符串 url 中搜索字符串值?
字符串网址:
http://localHost:8070/serviceCase=ActiveService
http://localHost:8070/serviceCase=KoplService
字符串值为:
ActiveService
我需要打印具有“ActiveService”的网址!
你可能会做这样的事情(如果你的 url 真的是一个字符串):
if ([url rangeOfString:value].location == NSNotFound){
dosomething
} else {
doSomethingElse
}
if([string hasSuffix:@"ActiveService"]) {
// Do your stuff
}
NSString *url = @"http://localHost:8070/serviceCase=ActiveService";
if ([url rangeOfString:@"ActiveService"].location != NSNotFound) {
NSLog(@"URL has the string");
}
我推荐下面的 NSString 类别。
NSString+Extend.h
#import <Foundation/Foundation.h>
@interface NSString (Extend)
- (BOOL)containsString:(NSString *)aString ignoringCase:(BOOL)flag;
- (BOOL)containsString:(NSString *)aString;
@end
NSString+Extend.m
#import "NSString+Extend.h"
@implementation NSString (Extend)
- (BOOL)containsString:(NSString *)aString
{
return [self containsString:aString ignoringCase:NO];
}
- (BOOL)containsString:(NSString *)aString ignoringCase:(BOOL)flag
{
unsigned mask = (flag ? NSCaseInsensitiveSearch : 0);
return [self rangeOfString:aString options:mask].length > 0;
}
@end
#import "NSString+Extend.h"
NSString *url = @"http://localHost:8070/serviceCase=ActiveService";
NSString *findString = @"ActiveService";
BOOL isContains = [url containsString:findString];
if(isContains)
{
//do stuff
}
else
{
//do stuff
}