天天看點

替換字元串中部分字元串,java

     public  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();   

    }