views:

135

answers:

2

If I have an NSString that is initially:

"ABCDE*FGHI"

How do I make it turn into

"FGHI"

In other words, everything from the asterisk onwards is kept.

Likewise, how would I turn it into:

"ABCDE"

(everything up to the asterisk is kept)

Thanks

A: 
NSString *myString = @"ABCDE*FGHI";
NSString *subString = [myString substringWithRange: NSMakeRange(0, [myString rangeOfString: @"*"].location)];
luvieere
I get these errors when compiling with that code: incompatible type for argument 1 of 'substringWithRange:', implicit declaration of function 'NSRangeMake'
cannyboy
@cannyboy Sorry, it was NSMakeRange, not NSRangeMake
luvieere
+4  A: 
NSString *myString = @"ABCDE*FGHI"; 
NSArray *myArray = [myString componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"*"]];
  • key 0 of myArray will contains @"ABCDE"
  • key 1 will contains @"FGHI"
Samuel