Delete characters and everything after the line

I am aware of replacing String strings, but this only works if I know exactly what I want to delete.

If I have a line as shown below:

"Hi-there-this-this-test & function = hi-there"

How to remove the '& feature' and everything after that?

Any help would be greatly appreciated. Thanks in advance!

EDIT: If you absolutely need to use REGEX, can someone show me how to use it? I know this is 10.7, but I'm fine with that. Better yet, an example of line trimming or using NSScanner?

Thanks again to everyone.

EDIT: The solution posted below is correct, but led to a crash for me. This is how I solved the problem:

NSString *newString = [[oldString componentsSeparatedByString: @"&feature="] objectAtIndex:0]; 
+6
source share
2 answers

This can be done without REGEX as follows:

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

See pattern matching, as well as regular expressions.

Oh wow, TIL is there no built-in way to do regular expressions in Cocoa's Objective-C? According to Regular Expressions in Cocoa Objective-C Application

Anyway, libs are mentioned in this question.

+1
source

Source: https://habr.com/ru/post/914282/


All Articles