objective-c get last 2 characters of a string?

人盡茶涼 提交于 2019-12-18 10:26:42

问题


Say I have a string:

NSString *state = @"California, CA";

Can someone please tell me how to extract the last two characters from this string (@"CA" in this example)


回答1:


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

should do it




回答2:


Swift 4:

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


来源:https://stackoverflow.com/questions/3483223/objective-c-get-last-2-characters-of-a-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!