zoukankan      html  css  js  c++  java
  • Java: Replace a string from multiple replaced strings to multiple substitutes

    Provide helper methods to replace a string from multiple replaced strings to multiple substitutes

    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    
    
    public class StringHelper {
    	
    	/**
    	 * This is test method to replace a string from:
    	 * aaa > zzz
    	 * 	 > /t
    	 * 
     > /r
    	 * 
     > /n
    	 */
    	public static void run()
    	{
    		
    		String[] replacedStrings = new String[] {"aaa", "	", "
    ", "
    "};
    		String[] replacements = new String[] {"zzz", "/t", "/r", "/n"};
    	
    		Pattern pattern = getReplacePattern(replacedStrings);
    		
    		// The result is "zzza/tb/rc/nd/te/nf"
    		System.out.println(replace("aaaa	b
    c
    d	e
    f", pattern, replacedStrings, replacements));
    		
    		// The result is "zzza/tb/rc/nd/te/nf/r"
    		System.out.println(replace("aaaa	b
    c
    d	e
    f
    ", pattern, replacedStrings, replacements));
    	}
    	
    	/**
    	 * Return a Pattern instance from a specific replaced string array.
    	 * @param replacedStrings replaced strings
    	 * @return a Pattern instance
    	 */
    	public static Pattern getReplacePattern(String[] replacedStrings)
    	{
    		if (replacedStrings == null || replacedStrings.length == 0) return null;
    		
    		String regex = "";
    		for (String replacedString : replacedStrings)
    		{
    			if (regex.length() != 0)
    			{
    				regex = regex + "|";
    			}
    			regex = regex + "(" + replacedString + ")";
    		}
    		regex = regex + "";
    
    		return Pattern.compile(regex, Pattern.DOTALL | Pattern.MULTILINE);
    	}
    	
    
    	/**
    	 * Replace a string.
    	 * @param value the string.
    	 * @param pattern the Pattern instance.
    	 * @param replacedStrings the replaced string array.
    	 * @param replacements the replacement array.
    	 * @return
    	 */
    	public static String replace(String value, Pattern pattern, String[] replacedStrings, String[] replacements)
    	{
    		if (pattern == null) return value;
    		if (replacedStrings == null || replacedStrings.length == 0) return value;
    		if (replacements == null || replacements.length == 0) return value;
    		
    		if (replacedStrings.length != replacements.length) 
    		{
    			throw new RuntimeException("replacedStrings length must same as replacements length.");
    		}
    		
    		Matcher matcher = pattern.matcher(value);
    		StringBuffer buffer = new StringBuffer();
    		int lastIndex = 0;
    		
    		while (matcher.find()) {
    			buffer.append(value.subSequence(lastIndex,  matcher.start()));
    			lastIndex = matcher.end();
    
    			String group = matcher.group();
    			 
    			for (int i = 0; i < replacedStrings.length; i++)
    			{
    				if (group.equals(replacedStrings[i]))
    				{
    					buffer.append(replacements[i]);
    					break;
    				}
    			}
    	     }
    		 
    		 buffer.append(value.subSequence(lastIndex, value.length()));
    		 return buffer.toString();
    	}
    }
    
  • 相关阅读:
    IBM MQ 学习
    spring中配置监听队列的MQ
    数据库优化(二)
    设计模式
    VBA学习笔记(2)--新建word文档并插入文字
    VBA代码分行
    excel保存时出现“请注意,您的文档的部分内容可能包含了文档检查器无法删除的个人信息”
    Excel VBA 操作 Word(入门篇)
    win10无法使用内置管理员账户打开应用
    五笔字根拆分规则_字根拆分方法
  • 原文地址:https://www.cnblogs.com/steven-yang/p/5345296.html
Copyright © 2011-2022 走看看