日期:2014-05-19 浏览次数:20744 次
public static void main(String[] args) { String str = "01123425243319"; StringBuilder sb = new StringBuilder(); for(int i=0;i<str.length();i++) { if(i!=0 && i%2==0) sb.append(","); sb.append(str.charAt(i)); } System.out.println(sb.toString()); }
------解决方案--------------------
试过的喔~完全OK
记得结贴呀~~
代码如下:
public static void main(String[] args) {
// TODO Auto-generated method stub
String aa="01123425243319";
String newStr="";
for(int i=0;i<aa.length();i++){
if(i==0||i%2==0){
newStr=newStr+aa.substring(i,i+2)+",";
System.out.println(newStr);
}
}
}
------解决方案--------------------
public static void main(String[] args) { String startStr = "011234252433193"; String endStr = "" ; if(startStr.length()%2!=0){ for(int i=0;i<startStr.length()-1;i++){ if(i%2==0){ endStr += startStr.substring(i, i+2)+","; } if(i==startStr.length()-2){ endStr += startStr.substring(startStr.length()-1, startStr.length()); } } }else{ for(int i=0;i<startStr.length()-1;i++){ if(i%2==0){ endStr += startStr.substring(i, i+2)+","; } } endStr = endStr.substring(0, endStr.length()-1); } System.out.println(endStr); }