在下面的代码中,我检查了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";
}发布于 2013-10-14 15:03:54
这很简单:
if (foundRange.location==NSNotFound) {
_testLabel.text = [_testLabel.text stringByReplacingOccurrencesOfString:@"A" withString:@"T"];
//And so on
}我只是注意到,您要将A更改为T,将T更改为A,您可能需要使用一个临时值。
比如,把A改成临时,把T改成A,把温度改成T。
https://stackoverflow.com/questions/19363201
复制相似问题