objective c - componentsSeparatedByCharactersInSet gives me a bunch of empty strings -
ok, first , foremost, using gnustep way learn objective-c, there may differences between gnu , apple implementation. being said, shouldn't affect question.
anyways, understand conundrum, please parse following code visual receptacles:
#import <cocoa/cocoa.h> int main() { nsautoreleasepool * pool = [[nsautoreleasepool alloc] init]; nsenumerator * lineenumerator = [[nsarray arraywithobjects: @"jim 1", @"steve 3", nil] objectenumerator]; nsstring * s; while((s = [lineenumerator nextobject])) { nsarray * parts = [s componentsseparatedbycharactersinset: [nscharacterset whitespaceandnewlinecharacterset]]; nslog(@"%@", parts); } [pool drain]; return no; }
and following output:
2010-10-07 10:03:50.809 a.out[24512] (jim, "", "", "", "", 1) 2010-10-07 10:03:50.812 a.out[24512] (steve, "", "", 3)
my expected output be:
2010-10-07 10:03:50.809 a.out[24512] (jim, 1) 2010-10-07 10:03:50.812 a.out[24512] (steve, 3)
but componentsseparatedbycharactersinset seems method comes close looking (by way, want prepared mixture of spaces, tabs, or other whitespace characters). there easy way standard libraries without writing new method?
i think you'll have use nsscanner:
while((s = [lineenumerator nextobject])) { nsmutablearray *parts = [[nsmutablearray alloc] initwithcapacity:1]; nsscanner *scanner = [nsscanner scannerwithstring:s]; nsstring *token; while ([scanner scanuptocharactersfromset:[nscharacterset whitespaceandnewlinecharacterset] intostring:&token]) { [parts addobject:token]; } nslog(@"%@", parts); [parts release]; }
Comments
Post a Comment