74

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?

node ninja
  • 31,796
  • 59
  • 166
  • 254

4 Answers4

134

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)];
Kris Markel
  • 12,142
  • 3
  • 43
  • 40
  • 8
    Be sure to check out No one in particular's answer below. My solution will return garbage for a large number of unicode characters. – Kris Markel Aug 27 '12 at 10:23
  • 2
    The answer does not tell the full story. NSString becomes tricky when you deal with emoji, skin tones, diacritical marks and CJK characters. https://www.objc.io/issues/9-strings/unicode/ reveals more details of weird NSString. – Yang.Y Nov 25 '15 at 01:29
41

If you just want to get one character from an a NSString, you can try this.

- (unichar)characterAtIndex:(NSUInteger)index;

Used like so:

NSString *originalString = @"hello";
int index = 2;
NSString *theCharacter = [NSString stringWithFormat:@"%c", [originalString characterAtIndex:index-1]];
//returns "e".
Albert Renshaw
  • 17,282
  • 18
  • 107
  • 195
joshualmf
  • 437
  • 4
  • 5
23

Your suggestion only works for simple characters like ASCII. NSStrings store unicode and if your character is several unichars long then you could end up with gibberish. Use

- (NSRange)rangeOfComposedCharacterSequenceAtIndex:(NSUInteger)index;

if you want to determine how many unichars your character is. I use this to step through my strings to determine where the character borders occur.

Being fully unicode able is a bit of work but depends on what languages you use. I see a lot of asian text so most characters spill over from one space and so it's work that I need to do.

No one in particular
  • 2,682
  • 2
  • 17
  • 20
  • The documentation page for this method has a useful code fragment to adjust any range to start and stop at the Unicode boundaries. http://developer.apple.com/library/ios/#documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html – JeremyP Oct 01 '10 at 09:07
  • "No one in particular"'s concern is valid but information given here is inaccurate and confusing. NSStrings are built upon UTF-16 units, a 2-byte encoding scheme, and string's length, index and range are expressed in such units. Single UTF-16 units cover the vast majority of characters in modern languages, including East Asian, and is much larger than ASCII. There are indeed code points that have to be represented as a pair of UTF-16 units (composed chars), such as characters from some ancient languages, or certain characters with accent marks. When dealing with these, NOIP's advice does help. – CodeBrew Nov 17 '13 at 18:13
  • Does UTF-16 cover all Chinese characters? – Awesome-o Oct 25 '14 at 02:10
0
NSMutableString *myString=[NSMutableString stringWithFormat:@"Malayalam"];
NSMutableString *revString=@"";

for (int i=0; i<myString.length; i++) {
    revString=[NSMutableString stringWithFormat:@"%c%@",[myString characterAtIndex:i],revString];
}
NSLog(@"%@",revString);
Neeraj Jerauld
  • 744
  • 6
  • 2