Remove Characters and Everything After from String

cocoa, objective-c

Solution

It can be done without REGEX like this:

NSString *string = @"hi-there-this-is-a-test&feature=hi-there";
    NSRange range = [string rangeOfString:@"&feature"];
    NSString *shortString = [string substringToIndex:range.location];

Problem

I am aware of replacing Strings of a String, but that only works if I know exactly what I want to remove. If I have a String like the following: "hi-there-this-is-a-test&feature=hi-there" How do I remove '&feature' and everything that comes after that? Any help would be greatly appreciated. Thanks in advance! EDIT: If absolutely necessary to use REGEX, could someone show me how to use it? I am aware it is 10.7 onwards but I'm fine with that. Even better, an example of String trimming or using the NSScanner? Thanks again everyone. EDIT: The solution posted below is the correct one, but resulted in a crash for me. This is how I solved the problem: ``` NSString *newString = [[oldString componentsSeparatedByString: @"&feature="] objectAtIndex:0]; ```

Original source

Related problems