tags:

views:

1615

answers:

4

I have two paragraph in a string. But the contents are having many spaces in between the paragraph string. I want to remove the spaces from this big paragraph string. I tried using NSString *outPutStr = [outPutStr stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]; But it doesn't remove empty spaces that are there in the paragraph string. It does nothing.

Can someone please guide me with sample?

thanks.

Clave/

+1  A: 

It sounds like you have single spaces between words that you want to keep, but you might want to get rid of a series of many spaces between paragraphs. There are multiple ways to do this, but here is one that's easy to code:

NSMutableString* newStr = [NSMutableString stringWithString:oldStr];
NSUInteger numReplacements;
do {
  NSRange fullRange = NSMakeRange(0, [newStr length]);
  numReplacements = [newStr replaceOccurrencesOfString:@"  " withString:@" "
                                               options:0 range:fullRange];
} while(numReplacements > 0);

Read about the replaceOccurrencesOfString:withString:options:range: method (of NSMutableString) to see some details.

This is not the most efficient, but it works, and if you're only working with a small number of not-huge strings, the speed of execution won't be significant.

By the way, you need to do the replacement multiple times because, for example, doing it just once could replace four consecutive spaces by two consecutive spaces, and you'd still be left with multiple spaces.

Tyler
Yes, as you said i wanted to do that way, correct. But my string is so big, it has 4 paragraphs with so many spaces. I don't know how to attach my content text file here, so that you can see and suggest based on that. Can i attach a file with comments?
Clave Martin
If this code works, there's no need to post your particular string. Is it not working the way you want? I don't know how to attach text files in stackoverflow (maybe not possible?) but, if you can post the string on a web page, you could add a link to that page.
Tyler
A: 

Replace all space characters surrounded by non-space characters with a character not likely to be in the string like '•'. (You can search the text to find such a character. After the good space characters, the ones you want to keep are replaced with that character, then go through and delete every space character (or replace them all with a single space). Finally, go through and replace the • characters with a space.

mahboudz
Can you please provide a code snippet?
Clave Martin
+2  A: 

If you use the awesome framework RegexKitLite, which is very easy to embed in an iPhone project:

NSString *cleanString = [yourStringWithWhiteSpaces stringByReplacingOccurancesOfRegex:@" +" withString:@" "];

This says replace anytime you find any space that is longer than 1 character with a single space. I dont know your source, but you could do the same for new lines and carriage returns by using the simple

NSString *cleanerString = [cleanString stringByReplacingOccurancesOfString:@"\n" withString:@""];
NSString *theCleanestString = [cleanerString stringByReplacingOccurancesOfString:@"\r" withString:@""];

Note* there are cleaner ways to do this in the code, combining them into 1 step, but this is a way to break it down so you can easily understand what is going on and helpful for debugging.

coneybeare
A: 

I would suggest adding a category on NSString to normalize strings in this way.

@implementation NSString (NormalizedString)

-(NSString*)stringByNormalizingCharacterInSet:(NSCharacterSet*)characterSet
                                   withString:(NSString*)replacement;
{
  NSMutableString* result = [NSMutableString string];
  NSScanner* scanner [NSScanner scannerWithString:self];
  while (![scanner isAtEnd]) {
    if ([scanner scanCharactersFromSet:characterSet intoString:NULL) {
      [result appendString:replacement];
    }
    NSString* stringPart = nil;
    if ([scanner scanUpToCharactersFromSet:characterSet intoString:&stringPart]) {
      [result appendString:stringPart];
    }
  }
  return [[result copy] autorelease];
}

@end
PeyloW