objective c - How to parse a string format like [***]***? -
i need parse string [abc]000
, , want array containing abc
, 000
. there easy way it?
i'm using code this:
nsstring *samplestring = @"[abc]000"; nsarray *sampleparts = [samplestring componentsseparatedbystring:@"]"]; nsstring *firstpart = [[[sampleparts objectatindex:0] componentsseparatedbystring:@"["] lastobject]; nsstring *lastpart = [sampleparts lastobject];
but it's inefficient , didn't check whether string in format [**]**.
for simple pattern, can parse like:
nsstring *s = @"[abc]000"; nsstring *firstpart = nil; nsstring *lastpart = nil; if ([s characteratindex: 0] == '[') { nsuinteger = [s rangeofstring:@"]"].location; if (i != nsnotfound) { firstpart = [s substringwithrange:nsmakerange(1, - 1)]; lastpart = [s substringfromindex:i + 1]; } }
or learn use nsscanner class.
Comments
Post a Comment