//Java拼接字符串时,去掉最后一个多余的逗号
String str[] = { "hello", "beijing", "world", "shenzhen" };
StringBuffer buf = new StringBuffer();
for (int i = 0; i < str.length; i++) {
buf.append(str[i]).append(",");
}
if (buf.length() > 0) {
//方法一 : substring
System.out.println(buf.substring(0, buf.length()-1));
//方法二 :replace
System.out.println(buf.replace(buf.length() - 1, buf.length(), ""));
//方法三: deleteCharAt
System.out.println(buf.deleteCharAt(buf.length()-1));
}
//不产生多余的逗号
StringBuffer s = new StringBuffer();
Iterator it = list.iterator();
while(it.hasNext()){
Object o = it.next();
if(s.length() > 0){
s.append(",");
s.append(o);
} else{
s.append(o);
}
}
String ss = s.toString();
System.out.println(ss);
标签:JAVA,StringBuffer,System,集合,length,字符串,buf,append,out From: https://www.cnblogs.com/liftsail/p/16943549.html