/** * @Title: decode.java * @Package cn.hyron * @Description: 三种方法,至于性能,要看变换的数据件数大小来判断 * if-else与switch性能参见本人blog的 * http://blog.csdn.net/ahhsxy/article/details/6598587 * @author daigj ahhsxycb@163.com * @date 2011/07/11 18:03:38 * @version V1.0 */ package cn.hyron; import java.util.Date; import java.util.HashMap; /** * @ClassName: decode * @Description: java处理字符串替换操作 * A->Z * 0->1 * 1->C * 2->B * 3->A * 4->D * 5->H * 6->G * 7->F * 8->E * 9->I * -->J * @author daigj ahhsxycb@163.com * @date 2011/07/11 18:03:38 * */ public class CopyOfdecode { public String pub = ""; public void decode(String str) { long startDate = System.currentTimeMillis(); System.out.println(startDate); HashMap<String, String> map = new HashMap<String, String>(); map.put("A", "Z"); map.put("0", "1"); map.put("1", "C"); map.put("2", "B"); map.put("3", "A"); map.put("4", "D"); map.put("5", "H"); map.put("6", "G"); map.put("7", "F"); map.put("8", "E"); map.put("9", "I"); map.put("-", "J"); for(int i =0; i< str.length(); i++){ if(map.containsKey(String.valueOf(str.charAt(i)))){ pub += map.get(String.valueOf(str.charAt(i))); }else{ pub += String.valueOf(str.charAt(i)); } } long endDate = System.currentTimeMillis(); System.out.println(endDate); System.out.println("Map containsKey Times:"+(endDate - startDate)); } public void decode1(String str) { pub = ""; long startDate = System.currentTimeMillis(); System.out.println(startDate); for(int i =0; i< str.length(); i++){ switch (str.charAt(i)){ case 'A': pub += 'Z'; break; case '0': pub += '1'; break; case '1': pub += 'C'; break; case '2': pub += 'B'; break; case '3': pub += 'A'; break; case '4': pub += 'D'; break; case '5': pub += 'H'; break; case '6': pub += 'G'; break; case '7': pub += 'F'; break; case '8': pub += 'E'; break; case '9': pub += 'I'; break; case '-': pub += 'J'; break; } } long endDate = System.currentTimeMillis(); System.out.println(endDate); System.out.println("Swtich case Times:"+(endDate - startDate)); } public void decode2(String str) { pub = ""; long startDate = System.currentTimeMillis(); System.out.println(startDate); for(int i =0; i< str.length(); i++){ if (str.charAt(i)== 'A'){ pub += 'Z'; }else if(str.charAt(i)== '0'){ pub += '1'; }else if(str.charAt(i)== '1'){ pub += 'C'; }else if(str.charAt(i)== '2'){ pub += 'B'; }else if(str.charAt(i)== '3'){ pub += 'A'; }else if(str.charAt(i)== '4'){ pub += 'D'; }else if(str.charAt(i)== '5'){ pub += 'H'; }else if(str.charAt(i)== '6'){ pub += 'G'; }else if(str.charAt(i)== '7'){ pub += 'F'; }else if(str.charAt(i)== '8'){ pub += 'E'; }else if(str.charAt(i)== '9'){ pub += 'I'; }else if(str.charAt(i)== '-'){ pub += 'J'; } } long endDate = System.currentTimeMillis(); System.out.println(endDate); System.out.println("if else Times:"+(endDate - startDate)); } public static void main(String[] args) { CopyOfdecode d = new CopyOfdecode(); d.decode("A50-0123456"); d.decode1("A50-0123456"); d.decode2("A50-0123456"); System.out.println(d.pub); } }
java处理字符串替换操作
最新推荐文章于 2023-05-08 22:12:43 发布