2012-07-02 122 views
-1

我需要在字符串末尾截斷一個逗號,有點像這樣:字符串截斷,特別是末尾的逗號。

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

代替whitespaceCharacterSet是有什麼樣commaCharacterSet?

回答

3

如果每個字符串的開頭有一個逗號/空格和結尾:

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

,如果你只是要修剪的逗號:這麼多

[NSCharacterSet characterSetWithCharactersInString:@","]; 
+0

!THX! – stackOverFlew