Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get last 2 characters of a string?

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

like image 225
dpigera Avatar asked Aug 14 '10 12:08

dpigera


People also ask

How do you take the last two characters of a string?

To get the last two characters of a string, call the slice() method, passing it -2 as a parameter. The slice method will return a new string containing the last two characters of the original string.

How do I get the last 3 characters of a string?

Getting the last 3 characters To access the last 3 characters of a string, we can use the built-in Substring() method in C#. In the above example, we have passed s. Length-3 as an argument to the Substring() method.

How do you get the last few characters of a string?

To get the last N characters of a string, call the slice method on the string, passing in -n as a parameter, e.g. str. slice(-3) returns a new string containing the last 3 characters of the original string. Copied! const str = 'Hello World'; const last3 = str.

How do I get the last two characters of a string in Python?

Use slice notation [length-2:] to Get the last two characters of string Python. For it, you have to get the length of string and minus 2 char.


1 Answers

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

should do it

like image 172
Ferruccio Avatar answered Sep 21 '22 01:09

Ferruccio