Java字符串批量替换处理


由于Java自有的String Replace函数在运算速度和内存消耗方面效果都不很理想,因此自己实现了一种批量的字符串替换程序


/**
 * 
 */
package com.sohu.websearch.util;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.Set;

/**
 * @author dinghui
 *
 */
public class BatchReplaceUtil {

	/**
	 * @param args
	 */
	private static class ReplaceInfo{
		public String from;
		public String to;
		public int length;
		public int offset;
		
		
		public ReplaceInfo(String from,String to,int length,int offset){
			this.from=from;
			this.to=to;
			this.length=length;
			this.offset=offset;
		}
	}
	
	private static ReplaceInfo next(String source,ReplaceInfo[] ris,int start,boolean global){		
		int match_failure=0;
		int last=0x7fff;
		int point=0;
	
		if(global==true){
			for (int i=0;i<ris.length;i++){
				int offset=source.indexOf(ris[i].from,start);
				if(offset==-1){
					match_failure++;
					ris[i].offset=0x7fff;
				}else{
					ris[i].offset=offset;
					if(offset<last){
						last=offset;
						point=i;
					}
				}
			}
		}else{
			for (int i=0;i<ris.length;i++){
				ReplaceInfo ri=ris[i];
				if(ri.offset==0x7fff)
					match_failure++;
				else{
					if(ri.offset<last){
						last=ri.offset;
						point=i;
					}
				}
			}
		}
		
		if(match_failure==ris.length)
			return null;
		else
			return ris[point];
	}
	
	public static String batchreplace(String source, HashMap<String,String> regrexes) throws Exception{		
		int start=0;
		
		ReplaceInfo[] ris=new ReplaceInfo[regrexes.size()];
		
		Set<Entry<String,String>> entrysets=regrexes.entrySet();
		Iterator<Entry<String,String>> entrys=entrysets.iterator();
		
		int index=0;
		
		while(entrys.hasNext()){
			Entry<String,String> entry= entrys.next();
			String from=entry.getKey();
			String to=entry.getValue();
			ris[index]=new ReplaceInfo(from,to,from.length(),0);
			index++;
		}
		
		if(index!=ris.length)
			throw new Exception();
		
		StringBuffer sb=new StringBuffer();
		
		boolean global=true;
		
		while(true){
			ReplaceInfo ri=next(source,ris,start,global);
			
			if(ri==null){
				if(start>0){
					/**
					 * new String() should pay attention to optimize JVM memory
					 */
					sb.append(new String(source.substring(start)));
					source=sb.toString();
				}
				break;
			}else{
				/**
				 * new String() should pay attention to optimize JVM memory
				 */
				sb.append(new String(source.substring(start, ri.offset))).append(ri.to);
				start=ri.offset+ri.length;
				
				int offset=source.indexOf(ri.from,start);
				
				if(offset==-1){
					ri.offset=0x7fff;
				}else{
					ri.offset=offset;
				}
				global=false;
			}
		}		
		return source;
	}
	
	public static void main(String[] args) throws Exception {
		// TODO Auto-generated method stub
		String s="123${r}${d}123${resultid}${d}${resultid}";
		
		HashMap<String,String> map=new HashMap<String,String> ();
		
		map.put("${r}", "r");
		map.put("${d}", "d");
		map.put("${resultid}", "resultid");
		map.put("${result}", "resultid");
		
		String sd=null;
		
		long s1=System.currentTimeMillis();
		
		for(int i=0;i<10000;i++){		
			 sd=batchreplace(s,map);
		}
		long e1=System.currentTimeMillis();
		
		System.out.println(e1-s1);
		System.out.println(sd);
	}
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值