運用場景:輸入名稱格仲,自動篩選去除其中的空格字段
代碼如下田弥,擴展類,導入直接使用
#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];