@interface NSString (DeletWhiteSpace) // 返回一个去掉前后空格的字符串或者下划线,如果自己是一个nil 返回@“” - (NSString *)trimmingWhiteSpace; - (NSString *)aTrimmingWhiteSpaceInString; @end #import "NSString+DeletWhiteSpace.h" @implementation NSString (DeletWhiteSpace) - (NSString *)trimmingWhiteSpace{ if (self) { return [selfstringByTrimmingCharactersInSet:[NSCharacterSetwhitespaceAndNewlineCharacterSet]]; } return nil; } - (NSString *)aTrimmingWhiteSpaceInString{ NSArray *stringArray = [selfcomponentsSeparatedByCharactersInSet:[NSCharacterSetwhitespaceAndNewlineCharacterSet]]; if (stringArray) { NSMutableString *string = [NSMutableStringstring]; for (NSString *str in stringArray) { if ([str trimmingWhiteSpace]) { [string appendString:[str trimmingWhiteSpace]]; } } return string; } return nil; } @end