iphoneuitextviewnscharacterset

Why whitespaceAndNewlineCharacterSet doesn't remove spaces?


This code SHOULD clean phone number, but it doesn't:

NSLog(@"%@", self.textView.text);
// Output +358 40 111 1111
NSString *s = [self.textView.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSLog(@"%@", s);
// Output +358 40 111 1111

Any ideas what is wrong? Any other ways to remove whitespacish characters from text string (except the hard way)?


Solution

  • Try this

    NSCharacterSet *dontWantChar = [NSCharacterSet whitespaceAndNewlineCharacterSet];
    NSString *string = [[self.textView.text componentsSeparatedByCharactersInSet:dontWantChar] componentsJoinedByString:@""];