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:@""];
Original: https://www.cnblogs.com/Free-Thinker/p/16148818.html
Author: brave-sailor
Title: iOS 字符串 去除空格
原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/546142/
转载文章受原作者版权保护。转载请注明原作者出处!