替换4个不同的字符

Dun*_*n11 0 c# replace

我正在尝试取用户输入,EG 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();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Kon*_*osa 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]);
Run Code Online (Sandbox Code Playgroud)

这将任何"ATCG"字符匹配替换为字典中的相应值.


Avi*_* P. 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;
}
Run Code Online (Sandbox Code Playgroud)