The issue I have is that I want to parse strings in order, so for instance, parsing “one three two”, adding that to another string, and printing “one three two”. I’m using rangeOfString:, but when I parse that string, it returns “one two three”. I know that the order of parsing in my case is the placement of the if statements, but how do I parse that string in order? Here is my code:
NSString *string = @"one three two";
NSString *newString;
if ([string rangeOfString:@"one"].location != NSNotFound)
{
newString = [newString stringByAppendingString:@"one"];
}
if ([string rangeOfString:@"two"].location != NSNotFound)
{
newString = [newString stringByAppendingString:@"two"];
}
if ([string rangeOfString:@"three"].location != NSNotFound)
{
newString = [newString stringByAppendingString:@"three"];
}
NSLog(@"%@",newString);
You are doing this wrong, you should use
componentsSeparatedByString:and sorting: