0

i have text message and I want to check whether it is containing text "http" or URL exists in that.

How will I check it?

3 Answers 3

6
NSString *string = @"xxx http://someaddress.com";
NSString *substring = @"http:";

Case sensitive example:

NSRange textRange = [string rangeOfString:substring];

if(textRange.location != NSNotFound){
    //Does contain the substring
}else{
    //Does not contain the substring
}

Case insensitive example:

NSRange textRange = [[string lowercaseString] rangeOfString:[substring lowercaseString]];

if(textRange.location != NSNotFound){
    //Does contain the substring
}else{
    //Does not contain the substring
}
Sign up to request clarification or add additional context in comments.

Comments

2

@Cyprian offers a good option.

You could also consider using a NSRegularExpression which would give you far more flexibility assuming that's what you need, e.g. if you wanted to match http:// and https://.

Comments

0

Url usually has http or https in it

You can use your custom method containsString to check for those strings.

- (BOOL)containsString:(NSString *)string {
    return [self containsString:string caseSensitive:NO];
}
- (BOOL)containsString:(NSString*)string caseSensitive:(BOOL)caseSensitive {
    BOOL contains = NO;
    if (![NSString isNilOrEmpty:self] && ![NSString isNilOrEmpty:string]) {
        NSRange range;
        if (!caseSensitive) {
            range =  [self rangeOfString:string options:NSCaseInsensitiveSearch];
        } else {
            range =  [self rangeOfString:string];
        }
        contains = (range.location != NSNotFound);
    }

    return contains;
}

Example :

[yourString containsString:@"http"]

[yourString containsString:@"https"] 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.