public static String replaceStr(String str){ StringBuffer buffer = new StringBuffer(str); buffer.replace(1, buffer.length(), "**"); return buffer.toString(); } public static String replaceStr2(String str){ StringBuffer buffer = new StringBuffer(str); buffer.replace(3, 7, "****"); return buffer.toString(); }
调用:
replaceStr("张振武");replaceStr2("18221801233");