1.去掉两端的空格
[str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] 2.去掉多余的空格
NSString *str = @" this is a test . ";
NSCharacterSet *whitespaces = [NSCharacterSet whitespaceCharacterSet];
NSPredicate *noEmptyStrings = [NSPredicate predicateWithFormat:@"SELF != ''"];
NSArray *parts = [str componentsSeparatedByCharactersInSet:whitespaces];
NSArray *filteredArray = [parts filteredArrayUsingPredicate:noEmptyStrings];
str = [filteredArray componentsJoinedByString:@" "];3.去掉所有空格
[str stringByReplacingOccurrencesOfString:@" " withString:@""]
本文详细介绍了如何使用Objective-C语言去除字符串两端的空格和多余的空字符,并提供了实例代码。重点突出编程技巧及其实用性。
286

被折叠的 条评论
为什么被折叠?



