ios - remove excess white space -


i have unfiltered string may or may not have excess amount of white spaces. example have 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 sample string depending on received server. string may perfect , there might more white space expected. want there regular white space between names , 1 \n @ end of each row. how can remove excess white space (more regular space) , more 1 new line?

i tried doing

stringreplaceoccurencesofstring  

but not cover cases.

can me? thank

there lots of fancy ways this, 1 of readable , easy understand ways (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 copied technique answer: https://stackoverflow.com/a/9715463/937822.
please up-vote answer if solution. :)

there more efficient ways this. if processing large amount of data , performance isn't enough, easiest loop through individual characters , process 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 duplicate spaces , newlines not copying them destination character array. char prevchar = 0; int y = 0; (int = 0; < 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 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 

Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

CSS3 Transition to highlight new elements created in JQuery -