【问题标题】:Check Objective-C String for specific characters检查 Objective-C 字符串中的特定字符
【发布时间】:2013-10-21 07:15:38
【问题描述】:

对于我正在开发的应用程序,我需要检查文本字段是否仅包含字母 A、T、C 或 G。此外,我想为任何其他输入的字符制作专门的错误消息。例如)“不要输入空格。”或“字母 b 不是可接受的值。”我读过其他几篇类似的帖子,但它们是字母数字的,我只想要指定的字符。

【问题讨论】:

    标签: ios objective-c xcode5 illegal-characters


    【解决方案1】:

    为您提供一种方法,绝非独一无二:

    NString 具有查找子字符串的方法,表示为位置和偏移量的NSRange,由给定NSCharacterSet 中的字符组成。

    字符串中应该包含的内容的集合:

    NSCharacterSet *ATCG = [NSCharacterSet characterSetWithCharactersInString:@"ATCG"];
    

    以及不应该发生的事情:

    NSCharacterSet *invalidChars = [ATCG invertedSet];
    

    您现在可以搜索由invalidChars 组成的任意字符范围:

    NSString *target; // the string you wish to check
    NSRange searchRange = NSMakeRange(0, target.length); // search the whole string
    NSRange foundRange = [target rangeOfCharacterFromSet:invalidChars
                                                 options:0 // look in docs for other possible values
                                                   range:searchRange];
    

    如果没有无效字符,则foundRange.location 将等于NSNotFound,否则您更改检查foundRange 中的字符范围并生成您的专门错误消息。

    你重复这个过程,根据foundRange更新searchRange,找出所有无效字符的运行。

    您可以将找到的无效字符累积到一个集合中(可能是NSMutableSet)并在最后产生错误消息。

    你也可以使用正则表达式,见NSRegularExpressions

    等等。高温

    附录

    有一个非常简单的方法可以解决这个问题,但我没有给出它,因为你给我的信件暗示你可能正在处理很长的字符串,并且使用上面提供的方法可能是一个值得的胜利。但是,在您发表评论后重新考虑一下,也许我应该将其包括在内:

    NSString *target; // the string you wish to check
    NSUInteger length = target.length; // number of characters
    BOOL foundInvalidCharacter = NO;   // set in the loop if there is an invalid char
    
    for(NSUInteger ix = 0; ix < length; ix++)
    {
       unichar nextChar = [target characterAtIndex:ix]; // get the next character
    
       switch (nextChar)
       {
          case 'A':
          case 'C':
          case 'G':
          case 'T':
             // character is valid - skip
             break;
    
          default:
             // character is invalid
             // produce error message, the character 'nextChar' at index 'ix' is invalid
             // record you've found an error
             foundInvalidCharacter = YES;
       }
    }
    
    // test foundInvalidCharacter and proceed based on it
    

    HTH

    【讨论】:

    • 如何检查一系列字符?如您所知,我仍在努力学习所有这些 Objective-C 的东西,还有很多。
    • 同样有很多方法,但在最简单的级别上,NSString 上有 characterAtIndex: 方法 - 范围为您提​​供起始索引和长度,因此简单的循环可以依次检查每个字符。还有一些方法可以为您提供NSString 的范围等。阅读NSString,也许还有NSMutableSet,文档页面。
    【解决方案2】:

    像这样使用 NSRegulareExpression。

    NSString *str = @"your input string";
    NSRegularExpression *regEx = [NSRegularExpression regularExpressionWithPattern:@"A|T|C|G" options:0 error:nil];
    NSArray *matches = [regEx matchesInString:str options:0 range:NSMakeRange(0, str.length)];
    for (NSTextCheckingResult *result in matches) {
        NSLog(@"%@", [str substringWithRange:result.range]);
    }
    

    对于选项参数,您必须查看文档以选择适合的参数。

    【讨论】:

    • for 循环到底在做什么?
    • 上述方法返回字符串中的所有匹配项,因此您必须循环它们并分别提取每个匹配项。
    • 我还注意到我没有提供模式!如果要匹配 A、T、C 或 G,请使用 @"A|T|C|G" 作为模式。
    【解决方案3】:
    猜你喜欢
    • 1970-01-01
    • 2011-07-08
    • 1970-01-01
    • 2013-10-19
    • 1970-01-01
    • 1970-01-01
    • 2017-12-16
    相关资源
    最近更新 更多