-3

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

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,
4

12 回答 12

3

尝试:

for (int i = 0; i < a.length; i++) {
    if ( i > 0 ) {
        sb.append(",");
    }
    sb.append(a[i]);
}
于 2013-03-25T11:36:14.420 回答
3

这样做

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

sb.append(a[a.length - 1]);
于 2013-03-25T11:36:21.940 回答
2
StringBuffer sb = new StringBuffer();
String separator = "";
for (int i = 0; i < a.length; i++) {
    sb.append(separator).append(a[i]);
    separator = ",";
}
于 2013-03-25T11:42:29.680 回答
2

有很多方法可以做到这一点,下面是一个使用正则表达式的解决方案String#replaceAll()

String s= "abc,";
s = s.replaceAll(",$", "");
于 2013-03-25T11:35:57.290 回答
1

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

String withComma= sb.toString();
String strWithoutLastComma = withComma.substring(0,withComma.lastIndexOf(","));
System.out.println(strWithoutLastComma);
于 2013-03-25T11:39:00.887 回答
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());

}
于 2013-03-25T11:37:31.293 回答
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());
于 2013-03-25T11:38:08.473 回答
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("]");
  }

}

使用分离数组、集合、迭代器、可迭代对象等的静态方法,你可以对这个类做更多的事情。

于 2013-03-25T11:52:29.770 回答
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));

}

或者

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());
}
于 2013-03-25T11:39:01.523 回答
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]);
}
于 2013-03-25T11:35:54.400 回答
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;
      }
}
于 2013-03-25T11:44:31.317 回答
0

试试这个

for (int i = 0; i < a.length; i++) {
    if(i == a.length - 1 )
    {
        sb.append(a[i]);
    } else {
        sb.append(a[i] + ",");
    }
}
于 2013-03-25T11:47:00.777 回答