zoukankan      html  css  js  c++  java
  • 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',
    }
  • 相关阅读:
    JS闭包
    webpack管理资源
    在webpack中使用配置文件
    webpack起步
    buuctf-MISC 面具下的flag
    Kali linux Steghide开源隐写工具
    buuctf-misc 九连环
    buuctf-Crypto rsarsa 1
    buuctf-web HardSQL 1
    buuctf-web Hack World 1
  • 原文地址:https://www.cnblogs.com/jacky1982/p/10299730.html
Copyright © 2011-2022 走看看