0

我想要做的是从 NSString 中获取 3 个字母的单词序列,并将它们更改为相应的单词。EX)AUG =“START”,AGC =“Asparagine”等。我的想法是将 NSString 更改为 NSArray,每个元素包含 3 个字母,然后我可以在 for 循环中引用它们,以创建一个新字符串与结果词。

例子:

NSDictionary *aminoReplaceDict = @{
                                       @"AAA" : @"Lysine",
                                       @"AAC" : @"Asparagine",
                                       @"AAG" : @"Lysine",
                                       @"AAU" : @"Asparagine",
                                       @"ACA" : @"Threonine",
                                       @"ACC" : @"Threonine",
                                       @"ACG" : @"Threonine",
                                       @"ACU" : @"Threonine",
                                       @"AGA" : @"Arginine",
                                       @"AGC" : @"Serine",
                                       @"AGG" : @"Arginine",
                                       @"AGU" : @"Serine",
                                       @"AUA" : @"Isoleucine",
                                       @"AUC" : @"Isoleucine",
                                       @"AUG" : @"START",
                                       @"AUU" : @"Isoleucine",
                                       @"CAA" : @"Glutamine",
                                       @"CAC" : @"Histidine",
                                       @"CAG" : @"Glutamine",
                                       @"CAU" : @"Histidine",
                                       @"CCA" : @"Proline",
                                       @"CCC" : @"Proline",
                                       @"CCG" : @"Proline",
                                       @"CCU" : @"Proline",
                                       @"CGA" : @"Arginine",
                                       @"CGC" : @"Arginine",
                                       @"CGG" : @"Arginine",
                                       @"CGU" : @"Arginine",
                                       @"CUA" : @"Leucine",
                                       @"CUC" : @"Leucine",
                                       @"CUG" : @"Leucine",
                                       @"CUU" : @"Leucine",
                                       @"GAA" : @"Glutamic Acid",
                                       @"GAC" : @"Aspartic Acid",
                                       @"GAG" : @"Glutamic Acid",
                                       @"GAU" : @"Aspartic Acid",
                                       @"GCA" : @"Alanine",
                                       @"GCC" : @"Alanine",
                                       @"GCG" : @"Alanine",
                                       @"GCU" : @"Alanine",
                                       @"GGA" : @"Glycine",
                                       @"GGC" : @"Glycine",
                                       @"GGG" : @"Glycine",
                                       @"GGU" : @"Glycine",
                                       @"GUA" : @"Valine",
                                       @"GUC" : @"Valine",
                                       @"GUG" : @"Valine",
                                       @"GUU" : @"Valine",
                                       @"UAA" : @"STOP",
                                       @"UAC" : @"Tyrosine",
                                       @"UAG" : @"STOP",
                                       @"UAU" : @"Tyrosine",
                                       @"UCA" : @"Serine",
                                       @"UCC" : @"Serine",
                                       @"UCG" : @"Serine",
                                       @"UCU" : @"Serine",
                                       @"UGA" : @"STOP",
                                       @"UGC" : @"Cysteine",
                                       @"UGG" : @"Tryptonphan",
                                       @"UGU" : @"Cysteine",
                                       @"UUA" : @"Leucine",
                                       @"UUC" : @"Phenylalanine",
                                       @"UUG" : @"START",
                                       @"UUU" : @"Phenylalanine",
                                       };
    NSMutableString *replaceString = [NSMutableString stringWithString:_trnaOut.text];
    for( NSString *replaceWord in [aminoReplaceDict allKeys] )
    {
        [replaceString replaceOccurrencesOfString:replaceWord withString:aminoReplaceDict[replaceWord] options:NSCaseInsensitiveSearch range:NSMakeRange(0, replaceString.length )];
    }
    _aminoOut.text = replaceString;

输出: 在此处输入图像描述

4

2 回答 2

2

这应该有效(省略与您的问题相同的字典):

 NSString *exampleString = @"AAACAACAU";
NSMutableArray *stringArray = [[NSMutableArray alloc] init];
for( int i = 0; i < exampleString.length; i += 3)
{
    NSString *substring = [exampleString substringWithRange:NSMakeRange(i, 3)];
    [stringArray addObject:substring];
}

NSMutableArray *replacedArray = [[NSMutableArray alloc] init];
for( NSString *key in stringArray)
{
    NSString *replacedWord = aminoReplaceDict[key];
    [replacedArray addObject:replacedWord];
}

NSString *replaceString = [replacedArray componentsJoinedByString:@", "];
NSLog(@"%@", replaceString);
于 2013-10-14T16:42:56.887 回答
1

@riley Lloyd,据我了解,您的要求如下:-

First you need to split your string into three characters:-
NSString *yourString = @"AUCGUAUCA";

//it will give you AUC
NSRange oneToThreeRange= NSMakeRange(0, 3);
NSString *oneToThree= [yourString substringWithRange:oneToThreeRange];


//it will give you GUA
NSRange fourToSixRange= NSMakeRange(3, 3);
NSString *fourToSix= [yourString substringWithRange:fourToSixRange];

//it will give you UCA
NSRange sixToNineRange= NSMakeRange(6, 3);
NSString *sixToNine= [yourString substringWithRange:sixToNineRange];

//Now add on your array and perform what you want to do

NSArray * youArr=[NSArray arrayWithObjects:oneToThree,fourToSix,sixToNine,nil];
NSLog(@"%@",youArr);
于 2013-10-14T17:39:58.127 回答