replace characters in a string

C#:

using System;
using System.Linq;

public class DnaStrand
{
    public static string MakeComplement(string dna)
    {
        return string.Concat(dna.Select(GetComplement));
    }

    public static char GetComplement(char symbol)
    {
        switch (symbol)
        {
            case 'A':
                return 'T';
            case 'T':
                return 'A';
            case 'C':
                return 'G';
            case 'G':
                return 'C';
            default:
                throw new ArgumentException();
        }
    }
}

Java: 

public class DnaStrand {
  public static String makeComplement(String dna) {
    char[] chars = dna.toCharArray();
    for(int i = 0; i < chars.length; i++) {
      chars[i] = getComplement(chars[i]);
    }
    
    return new String(chars);
  }
  
  private static char getComplement(char c) {
    switch(c) {
      case 'A': return 'T';
      case 'T': return 'A';
      case 'C': return 'G';
      case 'G': return 'C';
    }
    return c;
  }
}

JS:

function DNAStrand(dna) {
  return dna.replace(/./g, function(c) {
    return DNAStrand.pairs[c]
  })
}

DNAStrand.pairs = {
  A: 'T',
  T: 'A',
  C: 'G',
  G: 'C',
}
原文地址:https://www.cnblogs.com/jacky1982/p/10299730.html