NSString - stringByTrimmingCharactersInSet

来源:互联网 发布:大学生网络危害的案例 编辑:程序博客网 时间:2024/05/02 02:02
NSString - stringByTrimmingCharactersInSet:
Returns a new string made by removing from both ends of the receiver characters contained in a given character set.

Declaration
SWIFT
func stringByTrimmingCharactersInSet(_ set: NSCharacterSet) -> String
OBJECTIVE-C
- (NSString * _Nonnull)stringByTrimmingCharactersInSet:(NSCharacterSet * _Nonnull)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.

Discussion
Use whitespaceCharacterSet or whitespaceAndNewlineCharacterSet to remove whitespace around strings.

Availability
Available in iOS 2.0 and later.

去掉字符串前后的空格:
NSString *newString = [oldString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

0 0
原创粉丝点击