Here you can find the source of replaceOnce(String source, String subject, String object)
public static String replaceOnce(String source, String subject, String object)
//package com.java2s; public class Main { public static String replaceOnce(String source, String subject, String object) {/*w w w. j av a 2 s . co m*/ StringBuffer rtnStr = new StringBuffer(); String preStr = ""; String nextStr = source; if (source.indexOf(subject) >= 0) { preStr = source.substring(0, source.indexOf(subject)); nextStr = source.substring( source.indexOf(subject) + subject.length(), source.length()); rtnStr.append(preStr).append(object).append(nextStr); return rtnStr.toString(); } else { return source; } } public static int indexOf(String str, String searchStr) { if (str == null || searchStr == null) { return -1; } return str.indexOf(searchStr); } }