iOS 字符串 去除空格

2017-02-28  本文已影响0人  王_尧

NSString *str = @" this is a test . ";

去掉两端的空格

str = [str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

去掉多余的空格
NSCharacterSet *whitespaces = [NSCharacterSet whitespaceCharacterSet]; NSPredicate *noEmptyStrings = [NSPredicate predicateWithFormat:@"SELF != ''"]; NSArray *parts = [str componentsSeparatedByCharactersInSet:whitespaces]; NSArray *filteredArray = [parts filteredArrayUsingPredicate:noEmptyStrings]; str = [filteredArray componentsJoinedByString:@" "];
去掉所有空格

str = [str stringByReplacingOccurrencesOfString:@" " withString:@""];

上一篇 下一篇

猜你喜欢

热点阅读