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.
去掉字符串前后的空格:
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]];