我正在从 Web 服务获取字符串值。我需要删除字符串之间的空格。我怎样才能做到这一点?
示例:如果字符串是 "A ALL YEAR"
,则在 UILabel 文本中显示值时它必须变为 "A ALL YEAR"
。
我附上了下面的图片以进行说明。
最佳答案
NSString *yourString = @"A ALL YEAR";
NSCharacterSet *whitespaces = [NSCharacterSet whitespaceCharacterSet];
NSPredicate *noEmptyStrings = [NSPredicate predicateWithFormat:@"SELF != ''"];
NSArray *parts = [yourString componentsSeparatedByCharactersInSet:whitespaces];
NSArray *filteredArray = [parts filteredArrayUsingPredicate:noEmptyStrings];
yourString = [filteredArray componentsJoinedByString:@" "];
输出:- 一整年
希望你需要这个。如果这不起作用,请告诉我。
关于ios - 在 UI 标签中显示时如何修剪 NSString 之间的空格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39574126/