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).
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.
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.
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.
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.
NSString *code = [state substringFromIndex: [state length] - 2];
should do it
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With