比较从nsstring到不同字符集的字符

时间:2012-10-15 15:32:29

标签: objective-c ios nsstring nscharacterset

我想将nsstring中的每个字符逐个地与不同的nscharactersets进行比较,并根据它匹配的字符集执行不同的操作。

我可以使用for循环将每个字符分配给子字符串进行比较。

- (void) compareCharactersOfWord: (NSString *) word {

    for (int i = 0; i<[word length]; i++) {

        NSString *substring = [word substringWithRange:NSMakeRange(i,1)];


        //need to compare the substring to characterset here

    }
}

我也有两个角色

 setOne = [[NSCharacterSet characterSetWithCharactersInString:@"EAIONRTLSU"]invertedSet];

 setTwo = [[NSCharacterSet characterSetWithCharactersInString:@"DG"] invertedSet];

我在比较部分有点迷失了。我尝试了不同的方法,如“rangeOfCharacterFromSet”但我不断得到错误。在伪代码中,我需要像

这样的东西
if (setOne containsCharacterFrom substring) {

//do stuff here

} else if (setTwo containsCharacterFrom substring) {

//do other stuff here

}

2 个答案:

答案 0 :(得分:1)

您需要从字符串中提取每个字符(unichar)并使用[NSCharacterSet characterIsMember:]确定它是否属于NSCharacterSet

- (void) compareCharactersOfWord: (NSString *)word
{
    // These could be initialised globally to speed things up a little...
    NSCharacterSet *setOne = [[NSCharacterSet characterSetWithCharactersInString:@"EAIONRTLSU"] invertedSet];
    NSCharacterSet *setTwo = [[NSCharacterSet characterSetWithCharactersInString:@"DG"] invertedSet];

    for (NSUInteger index = 0; index < [word length]; index++)
    {
        unichar c = [word characterAtIndex:index];
        if ([setOne characterIsMember:c])
        {
            // c is a member of character set #1
        }
        else if ([setTwo characterIsMember:c])
        {
            // c is a member of character set #2
        }
        else
        {
            // c is a member of neither character set
        }
    }
}

答案 1 :(得分:1)

要查看您的某个集合中的“substring”变量,您可以这样做:

if ([substring rangeOfCharacterFromSet:setOne].location != NSNotFound) {
    // substring is in setOne
} else if ([substring rangeOfCharacterFromSet:setTwo].location != NSNotFound) {
    // substring is in setTwo
}

另一种选择是使用角色。

for (int i = 0; i<[word length]; i++) {
    unichar ch = [word characterAtIndex:i];

    if ([setOne characterIsMember:ch]) {
        // in setOne
    } else if ([setTwo characterIsMember:ch]) {
        // in setTwo
    }
}

第二个选项有一个很大的限制。它不适用于高于0xFFFF的Unicode字符。