如何在iOS上将字符串拆分为子字符串?

Chi*_*ong 83 string iphone substring objective-c ios

NSString从服务器收到了一个.现在我想把它拆分成我需要的子串.如何拆分字符串?

例如:

substring1:从第二个字符读取到第5个字符

substring2:从第6个字符读取10个字符.

cod*_*gic 222

您还可以使用NString的componentsSeparatedByString方法通过子字符串拆分字符串.

文档示例:

NSString *list = @"Norman, Stanley, Fletcher";
NSArray *listItems = [list componentsSeparatedByString:@", "];
Run Code Online (Sandbox Code Playgroud)

  • 您应该能够使用NSString的"componentsSeparatedByCharactersInSet:"来分割多个字符. (7认同)

Joe*_*vin 38

NSString有几个方法:

[myString substringToIndex:index];
[myString substringFromIndex:index];
[myString substringWithRange:range];
Run Code Online (Sandbox Code Playgroud)

有关更多信息,请查看NSString的文档.