在下面的代码中,我检查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";
}
答案 0 :(得分:3)
这很简单:
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.