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....

Comments

  1. Thanks for the hint and the lovely meme...love it :)

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. This comment has been removed by the author.

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

    whitespaceString = [whitespaceString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    while ([whitespaceString rangeOfString:@" "].location != NSNotFound)
    whitespaceString = [whitespaceString stringByReplacingOccurrencesOfString:@" " withString:@" "];
    NSLog(@"%@", whitespaceString);

    ReplyDelete
  5. Really amazing blog, I’d love to discover some extra information. Epikone

    ReplyDelete
  6. Very informative blog, Finally i got good solutions for Remove white spaces between NSString..

    ReplyDelete

Post a Comment