0

我想将 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

}
4

2 回答 2

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
        }
    }
}
于 2012-10-15T15:39:52.540 回答
1

要查看您的一组中的“子字符串”变量是否会执行以下操作:

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 字符。

于 2012-10-15T15:42:51.020 回答