拼接字符串时去掉最后⼀个多余逗号当我们遍历拼接字符串的时候,最后会多出⼀个我们添加的字符(⽐如逗号)。
⽅式⼀:
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.place(buf.length() - 1, buf.length(), ""));
//⽅法三: deleteCharAt
System.out.println(buf.deleteCharAt(buf.length()-1));
}
⽅式⼆:
StringBuilder sdb = new StringBuilder();
for ( int t = 0; t < memberLen; t++ )
{
memTemp = ( strMember[t] );
if ( memTemp != null )
{
if ( sbd.length > 0 )
{
sbd.append( "," ).append( memTemp );
}else{
sbd.append( memTemp );
}
}
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。