replace all in a string - Java java.lang

Java examples for java.lang:String Replace

Description

replace all in a string

Demo Code

//package com.java2s;

public class Main {
    public static void main(String[] argv) {
        String src = "java2s.com";
        String from = "oo";
        String to = "00";
        System.out.println(replaceall(src, from, to));
    }/*ww w.j  a  v a2s . co  m*/

    public static String replaceall(String src, String from, String to) {
        StringBuffer tmp = new StringBuffer(src);
        int length = from.length();
        int index = tmp.toString().indexOf(from);
        while (index > 0) {
            tmp.replace(index, index + length, to);
            index = tmp.toString().indexOf(from);
        }
        return tmp.toString();
    }
}

Related Tutorials