objective-c怎么将一个字符串分割成多个字符串

 我来答
水主沉浮6s
推荐于2017-09-30 · TA获得超过349个赞
知道小有建树答主
回答量:241
采纳率:0%
帮助的人:208万
展开全部
可以用NSString类的 - (NSArray *)componentsSeparatedByString:(NSString *)separator函数实现。
例子:
假如有一个字符串
NSString *list = @"Karin, Carrie, David";
可以用上面的函数得到一个字符串数组:

NSArray *listItems = [list componentsSeparatedByString:@", "];
这个数组就是把原来的字符串用","分割得到的多个字符串:
{ @"Karin", @"Carrie", @"David"" }

下面是苹果官方NSString类的说明:
链接地址:
https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/index.html#//apple_ref/occ/instm/NSString/componentsSeparatedByString:

Objective-C

- (NSArray *)componentsSeparatedByString:(NSString *)separator

Parameters

separator

The separator string.

Return Value

An NSArray object containing substrings from the receiver that have been divided by separator.

Discussion

The substrings in the array appear in the order they did in the
receiver. Adjacent occurrences of the separator string produce empty
strings in the result. Similarly, if the string begins or ends with the
separator, the first or last substring, respectively, is empty. For
example, this code fragment:

NSString *list = @"Karin, Carrie, David";
NSArray *listItems = [list componentsSeparatedByString:@", "];

produces an array { @"Karin", @"Carrie", @"David"" }.

If list begins with a comma and space—for example, @", Norman, Stanley, Fletcher"—the array has these contents: { @"", @"Norman", @"Stanley", @"Fletcher" }

If list has no separators—for example, "Karin"—the array contains the string itself, in this case { @"Karin" }.
本回答被提问者和网友采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式