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

14 comments:

  1. awsome!!
    this blog is excellent і rеally like studying your artiсles.

    Take a look at my page iPhone App Development usa
    web development services usa

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

    ReplyDelete
  3. This post is amazing. That tutorial is highly informative and glad to learn how to remove white spaces between NSString.
    http://bit.ly/1aFsG6a

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

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

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

    ReplyDelete
  7. 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
  8. If you too have expensive iPhone and want get it repair, just contact with these technicians and have your problem solved.

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

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

    ReplyDelete