Wednesday, 15 January 2014

ios - Extracting numbers from NSStrings -



ios - Extracting numbers from NSStrings -

i have many nsstring this:

@"this content. content of string may vary, length, , include characters, numbers y = 295.000000 x = 207.500000"

the part y = 295.000000 x = 207.500000 remains same, apart x , y numbers may change.

i need in way numbers , this:

coordsfinal.x = 295.000000; coordsfinal.y = 207.500000;

which have format of:

coordsfinal.x = [nsstring stringwithformat: @"%@", trimmed string];

any ideas??

you utilize regular expressions extract numbers:

nsstring *string = ...; // string nsregularexpression *regex = [nsregularexpression regularexpressionwithpattern:@"y = (\\d+.\\d+) x = (\\d+.\\d+)" options:0 error:null]; nstextcheckingresult *match = [regex firstmatchinstring:string options:0 range:nsmakerange(0, [string length])]; if (match) { nsrange yrange = [match rangeatindex:1]; nsstring *ystring = [string substringwithrange:yrange]; nsrange xrange = [match rangeatindex:2]; nsstring *xstring = [string substringwithrange:xrange]; nslog(@"x = %@, y = %@", xstring, ystring); }

output:

x = 207.500000, y = 295.000000

ios xcode

No comments:

Post a Comment