Pattern p = Pattern.compile("(?<=\\d)(?=(\\d\\d\\d)+$)");
Matcher m = p.matcher("1234567890");
System.out.println(m.replaceAll(","));
Result:1,234,567,890
java从控制台输入一串字符串,每隔3个字符添加一个“,”号,例如:2,222public static void main(String[] args) { BufferedReader br = new ,zreWnc
String str = "abcdefghij";
String newstr = "";
int size = ((str.length())%3 == 0) ? ((str.length())/3):((str.length())/3 + 1);
for(int i=0;i
if((i+1)==size){
endIndex = str.length();
}
if(i==0){
newstr += str.substring(i,endIndex);
}else{
newstr += ","+str.substring(i*3, endIndex);
}
}
System.out.print(newstr);
输出结果为abc,def,ghi ,j