当前位置: 代码迷 >> Java Web开发 >> java 替换字符串
  详细解决方案

java 替换字符串

热度:437   发布时间:2013-10-30 00:31:41.0
/** 
     * 功能描述:替换字符串 
     *  
     * @param from 
     *            String 原始字符串 
     * @param to 
     *            String 目标字符串 
     * @param source 
     *            String 母字符串 
     * @return String 替换后的字符串 
     */
    public static String replace(String from, String to, String source) { 
        if (source == null || from == null || to == null) 
            return null; 
        StringBuffer str = new StringBuffer(""); 
        int index = -1; 
        while ((index = source.indexOf(from)) != -1) { 
            str.append(source.substring(0, index) + to); 
            source = source.substring(index + from.length()); 
            index = source.indexOf(from); 
        } 
        str.append(source); 
        return str.toString(); 
    }

  相关解决方案