Re: Working with Strings
Re: Working with Strings
- Subject: Re: Working with Strings
- From: Brian Webster <email@hidden>
- Date: Sun, 15 Jul 2001 13:41:36 -0500
on 7/15/01 1:56 AM, email@hidden at
email@hidden wrote:
>
1. Can I compare the return value of NSString characterAtIndex:
>
with a normal character constant? i.e. will
>
>
if( [ myString characterAtIndex:0 ] == 'a' ) ...
>
>
evaluate correctly? If not, how can I compare characterAtIndex: with a
>
constant?
The characterAtIndex: returns a two-byte Unicode character value. For most
ASCII characters, you can compare them to the ASCII char cast to a unichar,
and that will work. So:
if([myString characterAtIndex:0] == (unichar)'a') ...
I don't know the details of the compatibility between ASCII and the first
255 values of Unicode. If you do some funky characters (high ASCII, etc.),
you probably want to check the Unicode spec at unicode.org to find exact
Unicode values.
>
2. What's the easiest way to append a single character to an
>
NSString? For constants, I'm using appendString: @"<character". What's
>
the simplest way to do this with variables?
unichar myCharacter;
NSMutableString *myString;
[myString appendString:[NSString stringWithCharacters:&myCharacter
length:1]];
--
Brian Webster
email@hidden
http://www.owlnet.rice.edu/~bwebster/