开发者

String tokenizer in Objective-C for iPhone application development

开发者 https://www.devze.com 2022-12-10 04:56 出处:网络
I am writing a string tokenizer in Objective-C for an iPhone application. I have the result as: 1|101|Y|103|Y|105|Y|107|Y|109|Y|111|Y|113|Y|115|Y|

I am writing a string tokenizer in Objective-C for an iPhone application.

I have the result as:

1|101|Y|103|Y|105|Y|107|Y|109|Y|111|Y|113|Y|115|Y|

I want to tokenize this string and display each of the values in tabular format. How am I to do it?

I want the result in a tabular format. Like:

102 Y
103 Y开发者_高级运维
..  ...


If by “tokenizing” you mean simply “splitting on the pipe-sign”, you can use the componentsSeparatedByString: method of NSString:

 NSString *original = @"1|101|Y|103|Y|105…";
 NSArray *fields = [original componentsSeparatedByString:@"|"];

“Displaying in a tabular format” doesn’t say much. If you want a classic table, see the UITableView class.


Not sure why you want this specifically in Objective-C (are you looking for a tokenizer class?), but generic strsep() resp. iso-c90 strtok() / strtok_r() do this and they exist on Mac OS X and on iPhone OS. Described here:

http://developer.apple.com/iPhone/library/documentation/System/Conceptual/ManPages_iPhoneOS/man3/strsep.3.html

These got code quotes as well.


NSString *string = @"1|101|Y|103|Y|105|Y|107|Y|109|Y|111|Y|113|Y|115|Y|"; NSArray *chunks = [string componentsSeparatedByString: @"|"];

0

精彩评论

暂无评论...
验证码 换一张
取 消