日期:2014-05-20 浏览次数:20702 次
public class Test { public static void main(String[] args) { String s1 = "10::101:ABC:102:GUANGZHOU :103:OK: "; String[] ss = s1.split(":"); StringBuilder stringBuilder = new StringBuilder(); int loops = ss.length - 1; for (int i = 0; i < loops; i += 2) { stringBuilder.append(ss[i]); stringBuilder.append(":"); stringBuilder.append(ss[i + 1]); stringBuilder.append("$"); stringBuilder.append(":"); } String s2 = stringBuilder.toString(); s2 = s2.substring(0, s2.length() - 2); System.out.println(s2); } }
------解决方案--------------------
"10::101:ABC:102:GUANGZHOU :103:OK: ".replaceAll("(:\\d)", "\\$$1")