开发者

String parsing in Objective-C

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜