java处理字符串替换操作

/**  
 * @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);
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值