Matoe Matoe - 2 months ago 33
Objective-C Question

Split NSString with multiple delimiters?

For text

bozo__foo!!bar.baz
, how to split an NSString containing this into
(bozo, foo, bar, baz)
?

That is, separe it in components with strings (delimiters)
__
,
!!
and
.
.

Answer

You can split the strings using NSCharacterSet. Try this

NSString *test=@"bozo__foo!!bar.baz";
NSString *sep = @"_!.";
NSCharacterSet *set = [NSCharacterSet characterSetWithCharactersInString:sep];
NSArray *temp=[test componentsSeparatedByCharactersInSet:set];
NSLog(@"temp=%@",temp);