-1

So, I have a vast quantity of NSStrings and my problem is I need to cut them into smaller strings at a specific point. This may sound complicated but what I need basically is this:

NSString *test =" blah blah blah - goo goo goo.";
NSString *str1 = "blah blah blah ";
NSString *str2 = "goo goo goo";

How do I code for when there's a hyphen for the string to just cut off there. Is there a way to do this? I found ways to cut of the string after a certain amount of letters but I need it at the hyphen every time.

Lorenzo B
  • 33,216
  • 24
  • 116
  • 190
user2069876
  • 21
  • 1
  • 4

3 Answers3

2
NSArray *arr = [string componentsSeparatedByString:@"-"];

should do the trick.

1

You could do this many ways. Two answers above show a few approaches. Many Objective-C solutions will include NSRange usage. You could also do more flexible things with NSScanner or NSRegularExpression. There is not going to be one right answer.

uchuugaka
  • 12,679
  • 6
  • 37
  • 55
0
NSString *cutString = [text substringFromIndex:3];
cutString = [text substringToIndex:5];
cutString = [text substringWithRange:NSMakeRange(3, 5)];
iiFreeman
  • 5,165
  • 2
  • 29
  • 42