Coding, How-To

iOS: Remove Leading and Trailing Whitespaces in NSString Object

This tip is especially useful because users might not notice the extra whitespace characters at the start and end of their text when they type into your text fields. The following code snippet will allow you to do that in iOS:

NSString *newString = [someString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];


You may use [NSCharacterSet whitespaceCharacterSet] if you do not need to trim away newline characters.



1 thought on “iOS: Remove Leading and Trailing Whitespaces in NSString Object

Leave a Reply

Your email address will not be published. Required fields are marked *