Fire Fist
Fire Fist

Reputation: 7060

How to get a character before last letter in NSString?

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

Answers (2)

holex
holex

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

Gabriele Petronella
Gabriele Petronella

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

Related Questions