运用场景:输入名称,自动筛选去除其中的空格字段
代码如下,扩展类,导入直接使用
#import <Foundation/Foundation.h>
@interface NSString (YT_character)
//去除字符串前后及中间的空格
+ (NSString *)stringByTrimmingCharacterString:(NSString *)string;
@end
#import "NSString+YT_character.h"
@implementation NSString (YT_character)
+ (NSString *)stringByTrimmingCharacterString:(NSString *)string {
string = [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
NSArray *arr = [string componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"self <> ''"];
arr = [arr filteredArrayUsingPredicate:predicate];
NSString *str = [arr componentsJoinedByString:@""];
return str;
}
@end
例如:
_zuheNameStr = [NSString stringByTrimmingCharacterString:_zuheNameStr];