Objective-C 去掉NSString 前后中空格
2016-01-04 10:19
525 查看
NSString 中缺少类似 Java 中的 .trim() 方法。直接去掉字符串类型值前后的空格。需要使用如下方法。
1、去掉前后空格
2、还有就是去除多于的空格,如下
1、去掉前后空格
NSString *cleanString = [dirtyString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
2、还有就是去除多于的空格,如下
NSString *theString = @" Hello this is a long string! "; NSCharacterSet *whitespaces = [NSCharacterSet whitespaceCharacterSet]; NSPredicate *noEmptyStrings = [NSPredicate predicateWithFormat:@"SELF != ''"]; NSArray *parts = [theString componentsSeparatedByCharactersInSet:whitespaces]; NSArray *filteredArray = [parts filteredArrayUsingPredicate:noEmptyStrings]; theString = [filteredArray componentsJoinedByString:@" "];
相关文章推荐
- 分享你最喜欢的技巧和提示(Xcode,Objective-C,Swift,C...等等)
- 黑马程序员——常用API详解之Object、Scanner、String
- objective-c 苹果官方NSLayoutConstraint 页面布局
- OBjective-c 第三方布局 Masonry
- GitHub Top 100 简介(Objective-C 项目 )
- Entity Framework 出现 "此 ObjectContext 实例已释放,不可再用于需要连接的操作" 的错误
- 每日一vim(18)Text-Object
- 2006-7有价值的Kean博客——Calling ObjectARX functions from a .NET Application(PInvoke)
- Objective-C开发者,一天学会swift
- 图像目标检测(Object Detection)原理与实现(三)
- 图像目标检测(Object Detection)原理与实现(二)
- 图像目标检测(Object Detection)原理与实现(一)
- [ES6] Object.assign (with defaults value object)
- [ES6] Objects create-shorthand && Destructuring
- IOS开发基础Object-C(13)—— protocol协议
- Objective-C语言——内存管理2(代码篇)
- Objective-C 5 NSString常用方法
- Objective-c语言_初始化类型不相同错误
- Objective-C语言——内存管理1
- Objective-c语言_练习题3