Monday, September 16, 2013

Remove white spaces between NSString

I was coding this app and was stuck with 
removing the spaces from NSString
when i ran into this class called as NSRegularExpression and guess what i helped me a lot, given below is the code which i used to remove white spaces between NSString object hope it helps






Remove white spaces between NSString code :

NSString *whitespaceString = @"This      is     a        string with         white                       spaces ";

NSError *error = nil;

NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@" +" options:NSRegularExpressionCaseInsensitive error:&error];

NSString *trimmedString = [regex stringByReplacingMatchesInString:whitespaceString options:0 range:NSMakeRange(0, [whitespaceString length]) withTemplate:@" "];

NSLog(@"%@",trimmedString);


Final String = This is a string with white spaces

Join us on facebook

Happy iCoding and have a great day....