views:

8661

answers:

2

I received an nsstring from the server. Now I want to split it into the substring I need. How to split the string?

For example:

substring1:read from the second character to 5th character

substring2:read 10 characters from the 6th character.

+9  A: 

NSString has a few methods for this:

[myString substringToIndex:index];
[myString substringFromIndex:index];
[myString substringWithRange:range];

Check the documentation for NSString for more information.

Joel Levin
+11  A: 

You can also split a string by a substring, using NString's componentsSeparatedByString method.

Example from documentation:

NSString *list = @"Norman, Stanley, Fletcher";
NSArray *listItems = [list componentsSeparatedByString:@", "];
codelogic
Can I split strings whose separate marks are different? e.g. @"A,B^C~D"
iPhoney
You should be able to use NSString's "componentsSeparatedByCharactersInSet:" to split on multiple characters.
codelogic