1

How can I search a string url for a string value?

String url:

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

String Value is:

ActiveService

I need to print the urls that do have "ActiveService"!

4 Answers 4

1

You could probably do something like this(if your url really is a string):

if ([url rangeOfString:value].location == NSNotFound){
     dosomething
} else {
     doSomethingElse
}
Sign up to request clarification or add additional context in comments.

Comments

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

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

Comments

0

I recommend below NSString Category.

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
}

2 Comments

It's an excellent solution, but it really depends on the number of string comparisons that have to be done. If we're only talking about a few places in the code, I think your solution is kinda overkill.
I know. but, When I test the performance compare to rangeOfString did not OverKill. When a later time using the KMP algorithm, i will create categories.
0
if([string hasSuffix:@"ActiveService"]) {
            // Do your stuff
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.