Search code examples
objective-cstringtruncation

truncation of strings, specifically a comma at the end.


I need to truncate a comma at the end of a string, sort of like this:

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

instead of whitespaceCharacterSet is there something like commaCharacterSet ?


Solution

  • If every string has a comma/white space at the beginning and end:

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

    if you just want to trim the comma:

    [NSCharacterSet characterSetWithCharactersInString:@","];