Word output based on NSString sequences

做~自己de王妃 提交于 2019-12-12 01:26:35

问题


What I want to do is take 3 letter word sequences from an NSString, and change them in to corresponding words. EX) AUG = "START", AGC = "Asparagine", etc. My thought was to change the NSString in to an NSArray with each element containing 3 letters, where I can then reference them in a for loop, to make a new string with the Resulting words.

Example:

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;

Output:


回答1:


This should work (leaving out the dictionary which is the same in your question):

 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);



回答2:


@riley Lloyd, As i understand your requirement below:-

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);


来源:https://stackoverflow.com/questions/19364787/word-output-based-on-nsstring-sequences

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!