objective-c怎么将一个字符串分割成多个字符串
2015-06-16 15:54
561 查看
可以用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" }.
相关文章推荐
- Objective-C中的NSObject对象经常使用到的方法
- NSObject源码
- Objective-C 正则表达式的使用
- Objective-C 基本类的Category (全而强大)
- 如果对象为空,java函数String.valueOf(Object obj)返回"null"字符串
- 比较C/C++和Objective-C的内存管理机制
- [Object-C]关于 UiView添加边框和设置边框的颜色
- Objective-C 中 +load 与 +initialize
- Active Object模式
- Function Objects Adapter(函数对象适配器)
- ProtocolBuffer for Objective-C 运行环境配置(真正测试过的)
- 多目标跟踪指南(Reference Guide-Multiple Object Tracking)
- mongoose — 检查ObjectId 是否存在于数组中
- Win7 OBJECT_HEADER之TypeIndex解析
- object-c里self与super的区别
- Object-C 中各数据类型转换 NSData转NSString,Byte,UIImage
- objective-c启用ARC时的内存管理
- GObject对象系统
- Java object serialization - Tutorial
- Android属性动画完全解析(中),ValueAnimator和ObjectAnimator的高级用法