Nick
Nick

Reputation: 5200

How to convert a single char into NSString?

I know how to convert a 'C' style string into a NSString, but is there an elegant (or even just easy!) way of converting a single 'C' char into a single character NSString?

I.e. 'Z' -> @"Z"

Short of creating a char *string and setting string[0] = myChar and then using

[NSString stringWithUTF8String:string]

I can't see a better way.

Upvotes: 0

Views: 558

Answers (3)

Martin R
Martin R

Reputation: 540105

Alternative solution (just for fun!), using a C99 "Compound Literal" with a Objective-C "Boxed Expression":

char myChar = 'A';
NSString *string = @((char[]){myChar, 0});

Upvotes: 3

Agent Chocks.
Agent Chocks.

Reputation: 1312

try using

char myChar = 'r';
NSString* string = [NSString stringWithFormat:@"%c" , myChar];

Upvotes: 3

Levi
Levi

Reputation: 7343

Try [NSString stringWithFormat:@"%c", myChar]

Upvotes: 6

Related Questions