Cut one string into two others in Objective-C
I have an NSURLRequest being made that to a server that returns a string.
string = [[NSMutableString alloc] initWithData:receivedData encoding:NSUTF8StringEncoding];
receivedData
is the mutable array that the downloaded data is stored in. Everything works fine.
I have now, however, added another value to that string. An example of the returned string would be 14587728000000 , 376.99
. Originally it was one value so I didn't have to do any splicing. But, now that I have another value, I want to be able to separate it into two different strings.
What should I do to separate the two values into different string? Some kind of search that goes till the first space, or something开发者_高级运维 like that. I have access to the server, and the string is generated in PHP so the separator can be anything.
You can do this with the NSString
componentsSeparatedByString
method:
NSString *string = @"14587728000000,376.99";
NSArray *chunks = [string componentsSeparatedByString: @","];
You can find some other common NSString
tricks (where I found this one) here.
Use -(NSArray *)componentsSeparatedByString:
and pass in the token to split by.
精彩评论