【问题标题】:Changing letters of更改字母
【发布时间】:2013-10-14 14:59:57
【问题描述】:

在下面的代码中,我检查了 NSString "newDNA" 以查看它是否只包含 ATCG。在下面的if语句中,如果foundRange.location==NSNotFound,我想改变字符串中的字母。如果字母是 A,我想把它改成 T,G 改成 C,C 改成 G,T 改成 A。我不太清楚该怎么做。

//Check characters
    NSCharacterSet *ATCG = [NSCharacterSet characterSetWithCharactersInString:@"ATCG"];
    NSCharacterSet *invalidChars = [ATCG invertedSet];
    //NSString *target; // the string you wish to check
    NSRange searchRange = NSMakeRange(0, newDNA.length); // search the whole string
    NSRange foundRange = [newDNA rangeOfCharacterFromSet:invalidChars
                                                 options:0 // look in docs for other possible values
                                                   range:searchRange];
    if (foundRange.location==NSNotFound) {
        _testLabel.text = @"YESSSS";
    }else{
        _testLabel.text = @"NOOOOOO";
    }

【问题讨论】:

标签: iphone ios objective-c ios7


【解决方案1】:

这很简单:

    if (foundRange.location==NSNotFound) {
        _testLabel.text = [_testLabel.text stringByReplacingOccurrencesOfString:@"A" withString:@"T"]; 
        //And so on
    }

我刚刚注意到您要将 A 更改为 T 并将 T 更改为 A,您可能需要使用临时值。

类似,将 A 更改为 temp,将 T 更改为 A,将 temp 更改为 T。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-12-11
    • 2014-02-02
    • 1970-01-01
    • 2020-04-16
    • 1970-01-01
    • 2017-02-19
    • 2023-03-21
    相关资源
    最近更新 更多