replace string by sub string using StringBuilder - Android java.lang

Android examples for java.lang:String Replace

Description

replace string by sub string using StringBuilder

Demo Code

import android.text.TextUtils;
import java.util.ArrayList;
import java.util.List;

public class Main{

    public static String replace(String originalString,
            String searchString, String replaceString) {
        StringBuilder sb = new StringBuilder(originalString);
        int index = sb.indexOf(searchString);
        while (index != -1) {
            sb.replace(index, index + searchString.length(), replaceString);
            index += replaceString.length();
            index = sb.indexOf(searchString, index);
        }//from  w  ww.j  av  a 2s .  co m
        return sb.toString();
    }

}

Related Tutorials