0

I'm trying to compare two strings

NSString strOne = @"Cat, Dog, Cow";
NSString strTwo = @"Cow";

How do I determine if strOne contains strTwo

4 Answers 4

4

Try using rangeOfString:

NSRange result = [strOne rangeOfString:strTwo];

From the documentation:

Returns an NSRange structure giving the location and length in the receiver of the first occurrence of aString. Returns {NSNotFound, 0} if aString is not found or is empty (@"").

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! Works like a charm. I'm posting my code below for others to use.
1

For anyone needing the code to check is a string exists within a string, here's my code thanks to fbrereto. This example checks to see if any string contained in an array of strings (stringArray) can be found within a string (myString):

int count = [stringArray count];

for (NSUInteger x = 0; x < count; ++x) {

    NSRange range = [self.myString rangeOfString:[stringArray objectAtIndex:x]];

    if (range.length > 0) {
        // A match has been found
        NSLog(@"string match: %@",[stringArray objectAtIndex:x]);
    }
}

1 Comment

Note that while that code will work, a better check is to see if range.position != NSNotFound (a constant value that will be returned when it cannot find anything).
1

I believe this is the correct syntax for checking if the range exists (correcting response from Kendall): range.location != NSNotFound

Comments

0

Gradually straying off topic, but I always explode my strings, which would mean just exploding it using your search string as a key and you can use the array count to see how many instances you have.

Just incase anyone is coming from a code language that uses "explode" to blow a string up into an array like me, I found writing my own explode function tremendously helpful, those not using "explode" are missing out:

- (NSMutableArray *) explodeString : (NSString *)myString key:(NSString*) myKey
{
    NSMutableArray *myArray = [[NSMutableArray alloc] init];
    NSRange nextBreak = [myString rangeOfString:myKey];
    while(nextBreak.location != NSNotFound)
    {
        [myArray addObject: [myString substringToIndex:nextBreak.location]];
        myString = [myString substringFromIndex:nextBreak.location + nextBreak.length];
        nextBreak = [myString rangeOfString:myKey];
    }
    if(myString.length > 0)
        [myArray addObject:myString];

    return myArray;
}

works like this:

[self explodeString: @"John Smith|Age: 37|Account Balance: $75.00" key:@"|"];

which will return this array:

[@"John Smith", @"Age: 37", @"Account Balance: $75.00"];

This lets you quickly pull out a specific value in a tight space, Like if you have a client and you want to know how much money he has:

[[self explodeString: clientData key: pipe] objectAtIndex: 1];

or if you wanted specifically the dollar amount as a float:

[[[self explodeString: [[self explodeString: clientData key: pipe] objectAtIndex: 1] key: @": "] objectAtIndex: 2] floatValue];

anyway I find arrays way easier to work with and more flexible, so this is very helpful to me. Additionally with a little effort you could make an "explodable string" data type for your private library that lets you treat it like a string or return an index value based on the key

ExplodableString *myExplodableString;
myExplodableString.string = @"This is an explodable|string";
NSString *secondValue = [myExplodableString useKey: @"|" toGetValue: index];

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.