iphone - How to use NSScanner or componentsSeperatedByString -


i have {"red","blue","green","yellow"} returned string. how process add array ?

#import <foundation/foundation.h>  int main (int argc, const char * argv[]) {     nsautoreleasepool * pool = [[nsautoreleasepool alloc] init];     nsstring* samplestring = @"{\"red\",\"blue\",\"green\",\"yellow\"}";     nsarray* components = [samplestring componentsseperatedbystring:@"\"{"];      [pool drain];     return 0; } 

updated code#

nsstring* samplestring = @"{\"red\",\"blue\",\"green\",\"yellow\"}";  nsmutablearray *rows = [nsmutablearray array];  // newline character set nsmutablecharacterset *removecharacterset = (id)[nsmutablecharacterset charactersetwithcharactersinstring:@"{(,}"]; [removecharacterset formintersectionwithcharacterset:[[nscharacterset whitespacecharacterset] invertedset]];  // characters important parser nsmutablecharacterset *importantcharactersset = (id)[nsmutablecharacterset charactersetwithcharactersinstring:@"\""]; [importantcharactersset formunionwithcharacterset:removecharacterset];  // create scanner, , scan string nsscanner *scanner = [nsscanner scannerwithstring:samplestring]; [scanner setcharacterstobeskipped:nil]; while ( ![scanner isatend] )  {             bool insidequotes = no;     bool finishedrow = no;     nsmutablearray *columns = [nsmutablearray arraywithcapacity:10];     nsmutablestring *currentcolumn = [nsmutablestring string];     while ( !finishedrow )      {         nsstring *tempstring;         if ( [scanner scanuptocharactersfromset:importantcharactersset intostring:&tempstring] ) {             [currentcolumn appendstring:tempstring];         }          if ( [scanner isatend] ) {             if ( ![currentcolumn isequaltostring:@""] ) [columns addobject:currentcolumn];             finishedrow = yes;         }         else if ( [scanner scancharactersfromset:removecharacterset intostring:&tempstring] ) {             if ( insidequotes ) {                 // add line break column text                 [currentcolumn appendstring:tempstring];             }             else {                 // end of row                 if ( ![currentcolumn isequaltostring:@""] ) [columns addobject:currentcolumn];                 finishedrow = yes;             }         }         else if ( [scanner scanstring:@"\"" intostring:null] ) {             if ( insidequotes && [scanner scanstring:@"\"" intostring:null] ) {                 // replace double quotes single quote in column string.                 [currentcolumn appendstring:@"\""];              }             else {                 // start or end of quoted string.                 insidequotes = !insidequotes;             }         }         else if ( [scanner scanstring:@"," intostring:null] ) {               if ( insidequotes ) {                 [currentcolumn appendstring:@","];             }             else {                 // column separating comma                 [columns addobject:currentcolumn];                 currentcolumn = [nsmutablestring string];                 [scanner scancharactersfromset:[nscharacterset whitespacecharacterset] intostring:null];             }         }     }     if ( [columns count] > 0 ) [rows addobject:columns]; } nslog(@"this string:%@",[rows objectatindex:0]); 

i got code http://www.macresearch.org/cocoa-scientists-part-xxvi-parsing-csv-data. output string:( red ), how rid of "(" ")" ?

here's need scan sample you've provided using instance of nsscanner:

nsscanner *scanner = [nsscanner scannerwithstring:@"{\"red\",\"blue\",\"green\",\"yellow\"}"]; nsmutablecharacterset *characterstoskip = [nsmutablecharacterset punctuationcharacterset]; [scanner setcharacterstobeskipped:characterstoskip];  nsmutablearray *substrings = [nsmutablearray array]; nsstring *substring = @""; while (![scanner isatend]) {     [scanner scanuptocharactersfromset:characterstoskip intostring:&substring];     [scanner scancharactersfromset:characterstoskip intostring:null];     [substrings addobject:substring]; }  nslog(@"%@", substrings); 

note if substituted parens curly braces, you'd need create array of strings sample be:

nsstring *samplestring = @"(\"red\",\"blue\",\"green\",\"yellow\")"; nsarray *strings = [samplestring propertylist]; nslog(@"%@", strings); 

...but i'm not clear on need accomplish.


Comments

Popular posts from this blog

javascript - Enclosure Memory Copies -

php - Replacing tags in braces, even nested tags, with regex -