1

I have an unfiltered string that may or may not have an excess amount of white spaces. So for example we have the following string:

NSString *testData= @"Jim Bob Marcus Trent   Mark\n\n 
                     Megan Laurie Andrea Katherine\n 
                     Dylan                Liam     Bernard\n 
                                                           \n   
                     Tim Thomas Britney    Marcus";

This is a sample string depending on what is received from the server. Sometimes the string may be perfect and sometimes there might be more white space then expected. Essentially I want there to be only regular white space between the names and only one \n at the end of each row. How can I remove excess white space (more then a regular space) and more then one new line?

I tried doing

stringReplaceOccurencesOfString 

But that does not cover all cases.

Can anyone help me? Thank you

Shardul
  • 4,266
  • 3
  • 32
  • 50
Teddy13
  • 3,824
  • 11
  • 42
  • 69
  • have you tried simply reading 1 char at a time and placing it in a new mutable string, and just skiping the current char if its a duplicate of space or line break? – Pochi May 20 '13 at 01:03
  • @rdelmar Thanks but as noted that is not objective c – Teddy13 May 20 '13 at 01:18
  • @ModernCarpentry agreed! – Teddy13 May 20 '13 at 01:19
  • @ChiquisDaCat I did a couple of days ago but could not getting it working so I scrapped it. Any idea how it would look? Thanks – Teddy13 May 20 '13 at 01:20
  • @Teddy13 You just have to iterate through the string, but try maddy's link, it is easier. – Pochi May 20 '13 at 02:25
  • Do you need to keep the newlines as you mention in your question? If not, then look here: http://stackoverflow.com/q/758212/937822. If you need the newlines, then it becomes (slightly) more complicated. – lnafziger May 20 '13 at 06:09
  • There's no `stringReplaceOccurencesOfString` method of `NSString`. *Which* method did you try? –  May 21 '13 at 04:42
  • By the way, all of the so called "duplicate" questions do not leave (one) newline at the end of each group of names like he has requested, and changes the question in perhaps not-so-obvious ways until you try to come up with a solution. Flagging to reopen as it is NOT an exact duplicate (although they contain good information related to the question). – lnafziger May 21 '13 at 18:12
  • Possible duplicate of [Collapse sequences of white space into a single character and trim string](https://stackoverflow.com/questions/758212/collapse-sequences-of-white-space-into-a-single-character-and-trim-string) – parvus Oct 05 '18 at 10:56

2 Answers2

3

There are lots of fancy ways to do this, but this is one of the most readable and easy to understand ways (as well as being pretty easy):

NSString *testData= @"Jim Bob Marcus Trent   Mark\n\n"
"Megan Laurie Andrea Katherine\n"
"Dylan                Liam     Bernard\n"
"\n"
"Tim Thomas Britney    Marcus";

// Remove duplicate spaces
while ([testData rangeOfString:@"  "].location != NSNotFound)
    testData = [testData stringByReplacingOccurrencesOfString:@"  " withString:@" "];

// Remove duplicate newlines
while ([testData rangeOfString:@"\n\n"].location != NSNotFound)
    testData = [testData stringByReplacingOccurrencesOfString:@"\n\n" withString:@"\n"];

NSLog(@"\n%@", testData);

// Results:
// Jim Bob Marcus Trent Mark
// Megan Laurie Andrea Katherine
// Dylan Liam Bernard
// Tim Thomas Britney Marcus

Note that I copied this technique from this answer: https://stackoverflow.com/a/9715463/937822.
Please up-vote his answer as well if you like this solution. :)

There are also more efficient ways to do this. If you are processing a large amount of data and the performance isn't good enough, then it would probably be easiest to loop through the individual characters and process the strings manually using lower level C techniques:

NSString *testData= @"Jim Bob Marcus Trent   Mark\n\n"
"Megan Laurie Andrea Katherine\n"
"Dylan                Liam     Bernard\n"
"\n"
"Tim Thomas Britney    Marcus";

// Create our C character arrays
const char *sourceString = [testData UTF8String];
int len = strlen(sourceString);
char *destString = malloc(len * sizeof(char));

// Remove the duplicate spaces and newlines by not copying them into the destination character array.
char prevChar = 0;
int y = 0;
for (int i = 0; i < len; i++)
{
    if (sourceString[i] == ' ' && prevChar == ' ')
        continue;
    if (sourceString[i] == '\n' && prevChar == '\n')
        continue;

    destString[y++] = sourceString[i];
    prevChar = sourceString[i];
}
destString[y] = '\0';

// Create the new NSString object
NSString *trimmedString = [NSString stringWithUTF8String:destString];

// Free our memory
free(destString);

NSLog(@"\n%@", trimmedString);

// Results:
// Jim Bob Marcus Trent Mark
// Megan Laurie Andrea Katherine
// Dylan Liam Bernard
// Tim Thomas Britney Marcus
Community
  • 1
  • 1
lnafziger
  • 25,760
  • 8
  • 60
  • 101
0

Take a look at NSRegularExpression or NSScanner. Also NSString has the following methods available

Replacing Substrings – stringByReplacingOccurrencesOfString:withString: – stringByReplacingOccurrencesOfString:withString:options:range: – stringByReplacingCharactersInRange:withString:

uchuugaka
  • 12,679
  • 6
  • 37
  • 55