zoukankan      html  css  js  c++  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));
    }
    

      

    方式二:

    StringBuilder sdb = new StringBuilder();
    for ( int t = 0; t < memberLen; t++ )
    {
        memTemp = stafferMap.get( strMember[t] );
        if ( memTemp != null )
        {
            if ( sbd.length > 0 )
            {
                sbd.append( "," ).append( memTemp );
            }else{
                sbd.append( memTemp );
            }
        }
    }
    

      

  • 相关阅读:
    Jzoj1307 Jail
    Jzoj1307 Jail
    Jzoj1306 Sum
    Jzoj1306 Sum
    Jzoj1279 解题
    Jzoj1279 解题
    Jzoj1277最高的奶牛
    Jzoj1277最高的奶牛
    Jzoj1155 有根树的同构(树的Rabin-Karp)
    Jzoj1155 有根树的同构(树的Rabin-Karp)
  • 原文地址:https://www.cnblogs.com/panchanggui/p/9847838.html
Copyright © 2011-2022 走看看