-1

我需要在字符串末尾截断逗号,有点像这样:

NSString *string = @" this text has spaces before and after ";
NSString *trimmedString = [string stringByTrimmingCharactersInSet:
                     [NSCharacterSet whitespaceCharacterSet]];

而不是 whitespaceCharacterSet 有类似 commaCharacterSet 的东西吗?

4

1 回答 1

3

如果每个字符串的开头和结尾都有逗号/空格:

NSRange range = NSMakeRange(1, [string length-1]);
NSString *trimmedString = [string substringWithRange:range];

如果您只想修剪逗号:

[NSCharacterSet characterSetWithCharactersInString:@","];
于 2012-07-02T05:42:53.063 回答