1

如何在字符串 url 中搜索字符串值?

字符串网址:

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

字符串值为:

ActiveService

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

4

4 回答 4

1

你可能会做这样的事情(如果你的 url 真的是一个字符串):

if ([url rangeOfString:value].location == NSNotFound){
     dosomething
} else {
     doSomethingElse
}
于 2012-08-07T11:31:02.793 回答
0
if([string hasSuffix:@"ActiveService"]) {
            // Do your stuff
}
于 2012-08-07T11:29:32.730 回答
0
NSString *url =  @"http://localHost:8070/serviceCase=ActiveService";

if ([url rangeOfString:@"ActiveService"].location != NSNotFound) {
    NSLog(@"URL has the string");
}
于 2012-08-07T11:30:17.737 回答
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
}
于 2012-08-07T11:41:49.090 回答