Get last 2 characters of a string?

Objective CNsstring

Objective C Problem Overview


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).

Objective C Solutions


Solution 1 - Objective C

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

should do it

Solution 2 - Objective C

Swift 4:

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiondpigeraView Question on Stackoverflow
Solution 1 - Objective CFerruccioView Answer on Stackoverflow
Solution 2 - Objective CNaishtaView Answer on Stackoverflow