Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get a single NSString character from an NSString

I want to get a character from somewhere inside an NSString. I want the result to be an NSString.

This is the code I use to get a single character at index it:

[[s substringToIndex:i] substringToIndex:1] 

Is there a better way to do it?

like image 437
node ninja Avatar asked Oct 01 '10 05:10

node ninja


People also ask

How do you get the first character of a string in Objective C?

You want: NSString *firstLetter = [codeString substringToIndex:1];

What is NS string?

A static, plain-text Unicode string object that bridges to String ; use NSString when you need reference semantics or other Foundation-specific behavior.

How do you escape in NSString?

This will escape double quotes in NSString: NSString *escaped = [originalString stringByReplacingOccurrencesOfString:@"\"" withString:@"\\\""];


1 Answers

This will also retrieve a character at index i as an NSString, and you're only using an NSRange struct rather than an extra NSString.

NSString * newString = [s substringWithRange:NSMakeRange(i, 1)]; 
like image 50
Kris Markel Avatar answered Sep 30 '22 17:09

Kris Markel