删除String末尾的逗号

时间:2013-03-25 11:34:04

标签: java

我正在尝试将HashSet转换为逗号分隔的分隔符 但问题是,我在字符串的末尾得到一个额外的逗号,如图所示。 请告诉我如何删除字符串末尾的额外逗号。

import java.util.HashSet;
import java.util.Set;

public class Test {

    private static Set<String> symbolsSet = new HashSet<String>();

    static {

        symbolsSet.add("Q1!GO1");
        symbolsSet.add("Q2!GO2");
        symbolsSet.add("Q3!GO3");

    }

    public static void main(String args[]) {
        String[] a = symbolsSet.toArray(new String[0]);
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < a.length; i++) {
            sb.append(a[i] + ",");
        }
        System.out.println(sb.toString());

    }

}

输出:

Q3!GOO3,Q2!GO2,Q1!GO1,

12 个答案:

答案 0 :(得分:3)

尝试:

for (int i = 0; i < a.length; i++) {
    if ( i > 0 ) {
        sb.append(",");
    }
    sb.append(a[i]);
}

答案 1 :(得分:3)

这样做

for (int i = 0; i < a.length - 1; i++) {
    sb.append(a[i] + ",");
}

sb.append(a[a.length - 1]);

答案 2 :(得分:2)

有很多方法可以做到这一点,下面是使用String#replaceAll()

的正则表达式解决方案
String s= "abc,";
s = s.replaceAll(",$", "");

答案 3 :(得分:2)

StringBuffer sb = new StringBuffer();
String separator = "";
for (int i = 0; i < a.length; i++) {
    sb.append(separator).append(a[i]);
    separator = ",";
}

答案 4 :(得分:1)

在您的给定方案中,String.lastIndexOf方法非常有用。

String withComma= sb.toString();
String strWithoutLastComma = withComma.substring(0,withComma.lastIndexOf(","));
System.out.println(strWithoutLastComma);

答案 5 :(得分:0)

   String str = sb.toString().substring(0, sb.toString().length()-1);

或仅在最后一个元素

时附加
  for (int i = 0; i < a.length; i++) {
    if ( i > 0 ) {
        sb.append(",");
    }
    sb.append(a[i]);
}

答案 6 :(得分:0)

public static void main(String args[]) {
    String[] a = symbolsSet.toArray(new String[0]);
    StringBuffer sb = new StringBuffer();
    for (int i = 0; i < a.length-1; i++) {
        //append until the last with comma
        sb.append(a[i] + ",");
    }
    //append the last without comma
    sb.append(a[a.length-1]);
    System.out.println(sb.toString());

}

答案 7 :(得分:0)

试试这个:

 for (int i = 0; i < a.length; i++) {
        if(i == a.length - 1) {
             sb.append(a[i]);  // Last element. Dont append comma to it
        } else {
        sb.append(a[i] + ","); // Append comma to it. Not a last element
        }
    }
    System.out.println(sb.toString());

答案 8 :(得分:0)

public static void main(String args[])
{
    String[] a = symbolsSet.toArray(new String[0]);

    StringBuffer sb = new StringBuffer();

    for (int i = 0; i < a.length; i++)
    {
        sb.append(a[i] + ",");
    }

    System.out.println(sb.toString().substring(0, sb.toString().length - 1));

}

OR

public static void main(String args[])
{
    String[] a = symbolsSet.toArray(new String[0]);

    StringBuffer sb = new StringBuffer();

    for (int i = 0; i < a.length; i++)
    {
        sb.append(a[i]);

        if(a != a.length - 1)
        {
            sb.append(",");
        }
    }

    System.out.println(sb.toString());
}

答案 9 :(得分:0)

试试这个,

for (int i = 0; i < a.length-1; i++) {
      if(a.length-1!=i)
            sb.append(a[i] + ",");
      else
      {
            sb.append(a[i]);
            break;
      }
}

答案 10 :(得分:0)

试试这个

for (int i = 0; i < a.length; i++) {
    if(i == a.length - 1 )
    {
        sb.append(a[i]);
    } else {
        sb.append(a[i] + ",");
    }
}

答案 11 :(得分:0)

你可以成长自己的Separator课程:

public class SimpleSeparator<T> {
  private final String sepString;
  boolean first = true;

  public SimpleSeparator(final String sep) {
    this.sepString = sep;
  }

  public String sep() {
    // Return empty string first and then the separator on every subsequent invocation.
    if (first) {
      first = false;
      return "";
    }
    return sepString;
  }

  public static void main(String[] args) {
    SimpleSeparator sep = new SimpleSeparator(",");
    System.out.print("[");
    for ( int i = 0; i < 10; i++ ) {
      System.out.print(sep.sep()+i);
    }
    System.out.print("]");
  }

}

使用静态方法分离数组,集合,迭代器,迭代等等,可以使用此类加载更多内容。