如何去掉字符串的前后TAB键和回车键
1个回答
展开全部
在objective-c中,如何去掉一个string 的前后的空格字符或某个特定字符呢?
如@ “ abc 123 ”字符串前后有空格,该如何去掉?
使用nsstring 的实例方法stringByTrimmingCharactersInSet :可以解决该问题。
方法如下:
C代码 收藏代码
[@" abc 123 " stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
NSString *newString = [oldString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSString 中该方法说明如下:
stringByTrimmingCharactersInSet:
Returns a new string made by removing from both ends of the receiver characters contained in a given character set.
- (NSString *)stringByTrimmingCharactersInSet:(NSCharacterSet *)set
Parameters
set
A character set containing the characters to remove from the receiver. set must not be nil .
Return Value
A new string made by removing from both ends of the receiver characters contained in set . If the receiver is composed entirely of characters from set , the empty string is returned.
这是典型的其他语言中trim 方法。我要问的是,如何去掉最左边的空格?又该如何去掉最右边的空格?
在NSString 的类中没有提供实现这类需求的方法,我们只能手工去添加这些方法。
如@ “ abc 123 ”字符串前后有空格,该如何去掉?
使用nsstring 的实例方法stringByTrimmingCharactersInSet :可以解决该问题。
方法如下:
C代码 收藏代码
[@" abc 123 " stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
NSString *newString = [oldString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSString 中该方法说明如下:
stringByTrimmingCharactersInSet:
Returns a new string made by removing from both ends of the receiver characters contained in a given character set.
- (NSString *)stringByTrimmingCharactersInSet:(NSCharacterSet *)set
Parameters
set
A character set containing the characters to remove from the receiver. set must not be nil .
Return Value
A new string made by removing from both ends of the receiver characters contained in set . If the receiver is composed entirely of characters from set , the empty string is returned.
这是典型的其他语言中trim 方法。我要问的是,如何去掉最左边的空格?又该如何去掉最右边的空格?
在NSString 的类中没有提供实现这类需求的方法,我们只能手工去添加这些方法。
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询