Trimming NSString Using stringByTrimmingCharactersInSet | iOS Programmer Guide

We can trim NSString in objective C using 'stringByTrimmingCharactersInSet'. Xcode snippet for trimming NSString in objective C is given below:

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

Another implementation by creating mutable string:

//make mutable string
NSMutableString *string2trim = [NSMutableString stringWithString:@" i needz trim "];

//pass it by reference to CFStringTrimSpace
CFStringTrimWhiteSpace((__bridge CFMutableStringRef) string2trim);

//You have trimmed the string!