Get last 2 characters of a string?

dpigera picture dpigera · Aug 14, 2010 · Viewed 44.6k times · Source

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

Answer

Ferruccio picture Ferruccio · Aug 14, 2010
NSString *code = [state substringFromIndex: [state length] - 2];

should do it