Reputation: 5290
I have an NSString
which consists of the text a user has entered into a UITextView
. As a result, the string may have leading and trailing newline characters, along with newline characters within the string (if they hit the Return key to type on a new line). I would like to remove any newline characters from the beginning and end of the string, leaving any within the string intact.
What would be the best way to go about this?
Upvotes: 0
Views: 2103
Reputation: 14677
text = [text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
This trims also whitespace.
If you want to keep whitespace, just use the newlineCharacterSet
instead.
Edited, as per @ghettopia correct suggestion about using the newlineCharacterSet instead of creating. Custom one.
Upvotes: 11