开发者

String parsing in Objective-C

开发者 https://www.devze.com 2023-02-11 20:25 出处:网络
When I have pf:/Abc/def/, how can I get the /Abc/def/? With Python, I can use string = \'pf:/Abc/def/\'

When I have pf:/Abc/def/, how can I get the /Abc/def/?

With Python, I can use

string = 'pf:/Abc/def/'
string.split(':')[1]

or even

string[3:]

What's开发者_StackOverflow社区 the equivalent function in Objective-C?


NSString *string = [NSString stringWithFormat:@"pf:/Abc/def/"];
NSArray *components = [string componentsSeparatedByString: @":"];
NSString *string2 = (NSString*) [components objectAtIndex:1];

Reference: http://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html#//apple_ref/occ/instm/NSString/componentsSeparatedByString:

componentsSeparatedByString will return an NSArray. You grab the object at a certain index, and type cast it to NSString when storing it into another variable.

0

精彩评论

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

关注公众号