Late Update

Is kind of an oxymoron but that’s ok because oxymorons tickle my brain in a good way. My favorite is definitely “random order”.

Anyway, I wrote some code that helps grab a section of a string (substring) from a larger string, without knowing what’s between the two strings. So it’s kind of useful for scanning/parsing a chunk of data. (look below for application)

The functions:

-(NSInteger)getScanLocation:(NSString *)content toString:(NSString *)lookup {

NSScanner *scanner = [NSScanner scannerWithString:content];

[scanner scanUpToString:lookup intoString:nil];

return [scanner scanLocation];

}

-(NSString *)grabInfo:(NSString *)content fromString:(NSString *)lookup {

NSInteger startScan = [self getScanLocation:content toString:lookup];

NSInteger lengthOfInfo = [content length];

NSRange start_range = NSMakeRange(startScan,(lengthOfInfo - startScan));

NSString *info = [content substringWithRange:start_range];

return info;

}

-(NSString *)getSection:(NSString *)content fromString:(NSString *)startString toString:(NSString *)endString {

NSString *first = [self grabInfo:content fromString:startString];

NSInteger endScan = [self getScanLocation:first toString:endString];

NSRange end_range = NSMakeRange(0, endScan);

NSString *final = [first substringWithRange:end_range];

return final;
}

As you could probably tell it’s not fully optimized or anything but for now it works. The application would look something like…

You have some NSString “data” e.g.

<example data>blahblah<data>/\/\/\120934u534814[j 109]<more crap><random order><chaos control>120938103453410

<more data><this is a lot of data we’re talking about here><what><yes><no>10348304803410922<oh man><test>

Now say I want what’s in bold, I would type:


NSString *data_section = [self getSection:data fromString:@"<more data>" toString:@"<yes>"];

Questions? Thoughts? Comments? Concerns?

Advertisements


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s