objective-c获取字符串的最后2个字符?

时间:2010-08-14 12:17:45

标签: iphone objective-c nsstring

说我有一个字符串:

NSString *state = @"California, CA";

有人可以告诉我如何从这个字符串中提取最后两个字符(在本例中为@“CA”)

2 个答案:

答案 0 :(得分:150)

NSString *code = [state substringFromIndex: [state length] - 2];

应该这样做

答案 1 :(得分:0)

Swift 4:

let code = (state as? NSString)?.substring(from: state.characters.count - 2)