Search code examples
objective-ccocoansstringnsrange

How do you read a string until a chosen string is found?


I have a string, NewString, that I want to only display text before the characters "Choices:". I only want to display the text before those characters. I tried this but obivously it didnt work. Any suggestions?

NewString = [NewString substringWithRange:NSMakeRange(0, @"Chocies:"];

Solution

  • NSRange range = [string rangeOfString:@"Choices:"];
    NSString *newString = [string substringToIndex:range.location];