Programming Tips - Java: Best way to insert a String into another String

Date: 2017sep28 Language: Java Q. Java: Best way to insert a String into another String A. Use StringBuilder.insert()
// Insert before * in a string. String insertDemo(final String in, final String more) { StringBuilder sb = new StringBuilder(in); int i = in.indexOf('*'); if (i < 0) return in; sb.insert(i, more); return sb.toString(); }
Of course, this isn't best in every case. But less error prone than using multiple substring's.