Bitching and Stiching iPhone Apps (almost) since 1974
RSS icon Email icon Home icon
  • Remove Whitespace from NSString

    Posted on January 28th, 2009 admin No comments

    In Cocoa Touch you always need to write more to achieve the same …

    NSString *string = @" spaces in front and at the end ";
    NSString *trimmedString = [string stringByTrimmingCharactersInSet:
                                      [NSCharacterSet whitespaceAndNewlineCharacterSet]];
    NSLog(trimmedString)

    While such a standard task might look excessively much code to write, you gain many additional possibilites of what you could want to trim away. NSCharacterSet also knows these other sets. 

    • alphanumericCharacterSet
    • capitalizedLetterCharacterSet
    • controlCharacterSet
    • decimalDigitCharacterSet
    • decomposableCharacterSet
    • illegalCharacterSet
    • letterCharacterSet
    • lowercaseLetterCharacterSet
    • newlineCharacterSet
    • nonBaseCharacterSet
    • punctuationCharacterSet
    • symbolCharacterSet
    • uppercaseLetterCharacterSet
    • whitespaceAndNewlineCharacterSet
    • whitespaceCharacterSet

    Leave a reply

    You must be logged in to post a comment.