NSString - stringByTrimmingCharactersInSet

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]];

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章