Gab
Gab

Reputation: 61

Objective-c use CharacterAtIndex

I want to use characterAtIndex in Objective-C to compare the character at a certain position and a character ('v' for example). The problem is that characterAtIndex seems to send an int and not a character. I tried to do something like this:

if ([string characterAtIndex:i] = @"v")

or also:

if (string[[string characterAtIndex:i]] .... )

I know that in some language you can go directly to a certain position in the string (like an array) to see the character, but it doesn't seems to work that way in Objective-C.

Thanks for you help !

Upvotes: 2

Views: 5504

Answers (2)

Chris Trahey
Chris Trahey

Reputation: 18290

I like @X-Slash's answer better (+1), but to be thorough I'll show how you would do this with only OOP cocoa (that is, with NSString's instead of c-strings).

if([[a substringWithRange:(NSRange){0,1}] isEqualToString:@"v"]) { ...

Where {0,1} is the first character, {1,1} is the second, etc.

Upvotes: 4

X Slash
X Slash

Reputation: 4131

characterAtIndex returns a char, not NSString object, so do this kind of thing instead,

if ([a characterAtIndex:i] == 'v') 

Upvotes: 8

Related Questions