简体   繁体   中英

Apostrophe Unichar Comparison Problems

I am currently looking at parts of a string like this ;

            if (thisChar == 'a')
            [self a];

        else if (thisChar == 'b')
            [self b];

        else if (thisChar == 'c')
            [self c];

.........

        else if (thisChar == '0')
        [self zero];

    else if (thisChar == '1')
        [self one];

    else if (thisChar == '2')
        [self two];

    else if (thisChar == '3')
        [self three];

.........

            else if (thisChar == '.')
        [self period];

    else if (thisChar == ',')
        [self comma];

    else if (thisChar == '?')
        [self qmark];

The problem arises though when I come to

else if (thisChar == ''')
        [self three];

What I want is the ' (apostrophe) sign but it won't let me do it. I know the reason why, but I don't know a way round it. Would doing this help?

NSString *apostropheString = [[NSString alloc] initWithFormat:@"'"];

unichar apostrophe = [apostropheString characterAtIndex:0];

Could I use an isEqualToString?

if ([apostrophe isEqualToString: thisChar]) 

Any help would be greatly appreciated!


I have found a way around it by doing the following;

1). Create an NNString with a single character which is an apostrophe

NSString *apostropheString = [[NSString alloc] initWithFormat:@"'"];

2). Get it to convert it to a unichar called "app", as it is zero-indexed and there is only one character the index is 0.

unichar app = [apostropheString characterAtIndex:0];

3). I then say if "thisChar" is equal to "app" run "qmark"

else if (thisChar == app)
        [self qmark];

I hope this helps for anyone with the same problem!

您必须使用反斜杠转义撇号: '\\''

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM