将String Url与String值进行比较

时间:2012-08-07 11:21:24

标签: iphone objective-c ios ipad

如何在字符串网址中搜索字符串值?

String url:

http://localHost:8070/serviceCase=ActiveService
http://localHost:8070/serviceCase=KoplService

字符串值为:

ActiveService

我需要打印有“ActiveService”的网址!

4 个答案:

答案 0 :(得分:1)

你可能会做这样的事情(如果你的网址确实是一个字符串):

if ([url rangeOfString:value].location == NSNotFound){
     dosomething
} else {
     doSomethingElse
}

答案 1 :(得分:0)

if([string hasSuffix:@"ActiveService"]) {
            // Do your stuff
}

答案 2 :(得分:0)

NSString *url =  @"http://localHost:8070/serviceCase=ActiveService";

if ([url rangeOfString:@"ActiveService"].location != NSNotFound) {
    NSLog(@"URL has the string");
}

答案 3 :(得分:0)

我建议在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
}