Write code to replace string using while loop and indexOf() method
//package com.book2s; public class Main { public static void main(String[] argv) { String str = "book2s.com"; String pattern = "o"; String replace = "O"; System.out.println(replace(str, pattern, replace)); }//from www.j a v a 2s .c om public static String replace(String str, String pattern, String replace) { int s = 0; int e; StringBuffer result = new StringBuffer(); while ((e = str.indexOf(pattern, s)) >= 0) { result.append(str.substring(s, e)); result.append(replace); s = e + pattern.length(); } result.append(str.substring(s)); return result.toString(); } }