截断字符串,特别是末尾的逗号。

时间:2012-07-02 05:37:40

标签: objective-c string truncation

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

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

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

1 个答案:

答案 0 :(得分:3)

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

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

如果您只想修剪逗号:

[NSCharacterSet characterSetWithCharactersInString:@","];