java中 StringBuffer字符串替换

来源:互联网 发布:婚庆公司源码 编辑:程序博客网 时间:2024/06/10 05:11
/*   * 替换字符串    * @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 bf = new StringBuffer("");       int index = -1;       while ((index = source.indexOf(from)) != -1) {         bf.append(source.substring(0, index) + to);         source = source.substring(index + from.length());         index = source.indexOf(from);       }       bf.append(source);       return bf.toString();     } 


 

原创粉丝点击