前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Java: Replace a string from multiple replaced strings to multiple substitutes

Java: Replace a string from multiple replaced strings to multiple substitutes

作者头像
绿巨人
发布2018-05-16 17:56:13
7390
发布2018-05-16 17:56:13
举报
文章被收录于专栏:绿巨人专栏绿巨人专栏

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

代码语言:javascript
复制
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 > /t
     * \r > /r
     * \n > /n
     */
    public static void run()
    {
        
        String[] replacedStrings = new String[] {"aaa", "\t", "\r", "\n"};
        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\tb\rc\nd\te\nf", pattern, replacedStrings, replacements));
        
        // The result is "zzza/tb/rc/nd/te/nf/r"
        System.out.println(replace("aaaa\tb\rc\nd\te\nf\r", 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();
    }
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2016-04-01 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档