-2

I am getting data from server and lot unexpected spaces between the words. like below

NSString* str = @"This   is Test  Result ". 

The extras must be remove like there must be only one space between This and is.

If we use the below only leading and trailing spaces will be removed, but I need different

NSString *trimmedString = [str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
Shaharyar
  • 12,254
  • 4
  • 46
  • 66
kiri
  • 1,977
  • 5
  • 27
  • 55

1 Answers1

2

You can try this,

NSString *properString= [str stringByReplacingOccurrencesOfString: @"[ \t]+"
                                                             withString: @" "
                                                                options: NSRegularExpressionSearch
                                                                  range: NSMakeRange(0, str.length)];

your result will be : "This is Test Result "

Vivek Shah
  • 430
  • 5
  • 22