Monday, 21 October 2013

remove excess white spaces | new line or \n | Trimming space, new line from NSString

NSString *testString = @"General Work Times change week-to-week.\n Please visit\n http://jabbleashish.blogspot.in/ \n for the latest schedule.\n\nOffice Hours:\n5 days a week except holidays.\n10Am-8Pm \n  or any late nights.";
NSLog(@"str : %@ -- %i",testString,testString.length);
// Remove duplicate spaces
 while ([testString rangeOfString:@"  "].location != NSNotFound)
       testString = [testString stringByReplacingOccurrencesOfString:@"  " withString:@" "];
// Remove duplicate multiple newlines
  while ([testString rangeOfString:@"\n\n"].location != NSNotFound)
        testString = [testString stringByReplacingOccurrencesOfString:@"\n\n" withString:@"\n"];
//Remove duplicate new line
   while ([testString rangeOfString:@"\n"].location != NSNotFound)
        testString = [testString stringByReplacingOccurrencesOfString:@"\n" withString:@" "];
NSLog(@"\n%@ -- %i", testString,testString.length);
[NSCharacterSet whitespaceAndNewlineCharacterSet]] Not works for me.

No comments:

Post a Comment