Reputation: 7060
I have NSString
with input Value from keyboard.
Eg.
NSString *myText = @"Apple";
In my case , i want to get a word before last letter.
For above eg , i want to get only l
letter before e
letter.
How can i get it?
Upvotes: 1
Views: 172
Reputation: 24041
that may be a useful implenentaion as well:
NSString *_string = @"string";
NSString *_letter = nil;
if (_string.length > 1) {
[_string substringWithRange:NSMakeRange(_string.length - 2, 1)];
}
it does not crash either, when the string is not long enough.
Upvotes: 1
Reputation: 108169
NSString *text = @"Apple";
unichar c = [text characterAtIndex:text.length - 2];
If you need a NSString
NSString *character = [NSString stringWithCharacters:&c length:1];
Upvotes: 2