How can I substring an NSString with the last x characters? Right to left.
In python it takes only one line!
>>> "string"[-4:]
'ring'
How can I substring an NSString with the last x characters? Right to left.
In python it takes only one line!
>>> "string"[-4:]
'ring'
It should be a one liner in Objective-C, too:
NSString *substring = [string substringFromIndex:[string length] - x];
You can find the answer you're looking for by checking out the documentation for NSString from the apple.developer.com website.
Specifically, you can use the substringFromIndex:
method.
For example, if you already have a string called originalString
and you
want to obtain a string that has the last x characters. You can do:
NSString* slicedString = [originalString substringFromIndex: originalString.length-x]
This should return a string with the last x characters.
int index = **Some integer value**
NSString *string = [originalString substringFromIndex:index];
NSString *string = [originalString substringToIndex:index];
NSString *substring = [initialstring substringFromIndex:[intialstring length] - x];