10

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];
Peter Hosey
  • 95,783
  • 15
  • 211
  • 370
Cristian
  • 6,765
  • 7
  • 43
  • 64
  • While technically a solution, that solution does more work than it needs to (namely, creates an array). You should go back to rdelmar's solution and investigate what you were doing that caused it to crash, because you're probably still doing it and it will cause other problems. – Peter Hosey Apr 27 '12 at 01:17
  • thanks v much for your help but believe me i've taken a good look and i've just created a string, filled it with a URL, converted it, trimmed it, and then using this solution crashes. I'm out of my depth trying to figure out why it crashes. – Cristian Apr 27 '12 at 01:32
  • 1
    As I said, you should ask a question about that. You can't learn anything useful by running from every crash you encounter; when you get a crash, you should tackle it head-on and learn from what you find. – Peter Hosey Apr 27 '12 at 01:34
  • Why don't you post the code that causes the crash, so we can take a look at it. – rdelmar Apr 27 '12 at 02:34

2 Answers2

23

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];
rdelmar
  • 103,982
  • 12
  • 207
  • 218
  • @Cristian: Then you're doing something else wrong. This is the correct solution. You should post a separate question about the crash. – Peter Hosey Apr 27 '12 at 01:12
  • ok thanks anyway, i really don't understand why it's crashing, an NSLog on my string shows the correct output, but if I use this solution it crashes.. strange – Cristian Apr 27 '12 at 01:13
  • Also crash when i put this method – Genevios Jan 13 '19 at 16:47
1

Look up pattern-matching, aka regular expressions.

Oh, wow, TIL there's no built-in way to do regular expressions in an Objective-C Cocoa? according to Regular expressions in an Objective-C Cocoa application

Anyway, there are libs mentioned in that question.

Community
  • 1
  • 1
MarkHu
  • 1,694
  • 16
  • 29
  • is there no built-in way? like 'stringByReplacingOccurrencesOfString() but delete everything after? – Cristian Apr 27 '12 at 00:31
  • Well, you could code around it with your own function that does a built-in call to get the position of the matched string, then trimming off the string at that point. And if you have a simple enough environment, then that would be fine. But once your project grows, then you will be cut-n-pasting that shared code... why not use a robust lib? – MarkHu Apr 27 '12 at 00:36
  • It's a small project, and this code would be included in a loop function, and there is only one specific occurrence I need to get rid of, which is removing '&feature' and everything after, this won't change. If you could provide an example of finding that and then trimming off I would be very grateful! – Cristian Apr 27 '12 at 00:40
  • Cocoa got support for regular expressions in Lion. Cocoa Touch got it in iOS 4. – Peter Hosey Apr 27 '12 at 01:13