替换4个不同的字符

时间:2015-03-24 22:18:24

标签: c# replace

我试图接受用户输入,E.G。 ATCG并将字母替换为TAGC。这些是DNA互补基因。例如,如果用户要输入ATCGGGC,则会输出TAGCCCG。我设法替换了1个角色,但我不确定如何让其他角色替换。

namespace DNA_Replication_EASY
{
    class Program
    {

        static string input1;


        public static string InputBaseWithSpaces()
        {
            return string.Join(" ", input1.ToCharArray());
        }

        public static string OpposingBases()
        {
            string outputBases1 = input1.Replace("A", "T");
            return outputBases1;
        }

        static void Main(string[] args)
        {

            Console.WriteLine("Please type out your DNA strand bases E.g. A T C G C A T G");
            input1 = Console.ReadLine();
            Console.WriteLine(InputBaseWithSpaces());
            Console.WriteLine(OpposingBases());

            Console.ReadLine();
        }
    }
}

8 个答案:

答案 0 :(得分:5)

使用Regex.Replace根据字典(地图)替换字符串:

Dictionary<string, string> complementary = new Dictionary<string,string>()
{
    { "A", "T" },
    { "T", "A" },
    { "C", "G" },
    { "G", "C" }

};
string input = "ATCG";
string result = Regex.Replace(input, "[ATCG]", match => complementary[match.Value]);

这取代了任何&#34; ATCG&#34;字符与字典中的对应值匹配。

答案 1 :(得分:5)

string MakeOpposite(string sourceString) {
    var opposites = new Dictionary<char, char> {
        { 'A', 'T' },
        { 'T', 'A' },
        { 'G', 'C' },
        { 'C', 'G' }
    };
    var oppositeString = new string(sourceString.Select(x => opposites[x]));
    return oppositeString;
}

答案 2 :(得分:1)

我会在Char数组上使用foreach和switch语句来替换每个字母。

foreach (char base in strand)
{
    switch (base.ToString())
    {
        case "g":
            //replace code
            break;
        case "c":
            //replace code
            break;
    }
}

答案 3 :(得分:1)

您应该编写一个例程来逐个字符地进行替换(不要使用string.replace方法)。

private string ConvertDNA(string original)
{
        StringBuilder newone = new StringBuilder();
        foreach(char c in original)
        {
            switch(c)
            {
                case 'A':
                    newone.Append('T');
                    break;
                case 'T':
                    newone.Append('A');
                    break;
                case 'C':
                    newone.Append('G');
                    break;
                case 'G':
                    newone.Append('C');
                    break;
                default:
                    newone.Append(c);
                    break;
            }       
        }
        return newone.ToString();
}

请注意,如果您的原始字符串是某些形式的Unicode,这可能会做有趣的事情。你应该在其他答案中使用stringbuilder而不是+ =语法,因为它更有效。

答案 4 :(得分:1)

将其转换为char数组并替换原位

string  input = "ATCG";
//TAGC

char[] inputChar = input.ToCharArray();

for(int i=0;i< inputChar.Length; i++)
{
    switch(inputChar[i])
    {
        case 'A':
            inputChar[i]='T';
            break;
        case 'T':
            inputChar[i]='A';
            break;
        case 'G':
            inputChar[i]='C';
            break;
        case 'C':
            inputChar[i]='G';
            break;
    }
}
var output =new string(inputChar);

答案 5 :(得分:0)

一种方法是使用Switch语句

public static string OpposingBases()
    {
        string outputBases1;
        foreach(var s in input1)
        {            
          switch (s)
          {
            case "A":
              outputBases1 +="T";
              break;
            case "T":
              outputBases1 +="A";
              break;
            case "C":
              outputBases1 +="G";
              break;
            case "G":
              outputBases1 +="C";
              break;
            case " ":
              outputBases1 +=" ";
              break;
            default:
              break;
          }
         }
        return outputBases1;
    }

答案 6 :(得分:0)

简单的解决方案,但你可以做得更好:

code = code.Replace("A","x").Replace("C","y").Replace("T","A").Replace("G","C").Replace("x","T").Replace("y","G");

答案 7 :(得分:0)

你可以这样做

    public static string OpposingBases()
    {
        var baseDictionary = new Dictionary<char, char>()
        {
            {'A', 'T'},
            {'T', 'A'},
            {'C', 'G'},
            {'G', 'C'}
        };
        return input1.Where(baseDictionary.ContainsKey).Aggregate("", (current, c) => current + baseDictionary[c]);
    }