删除String末尾的逗号

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

import java.util.HashSet; import java.util.Set; public class Test { private static Set symbolsSet = new HashSet(); 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, 

这样做

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

试试:

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

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

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

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

 String withComma= sb.toString(); String strWithoutLastComma = withComma.substring(0,withComma.lastIndexOf(",")); System.out.println(strWithoutLastComma); 
  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]); } 
 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()); } 

试试这个 :

  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()); 

尝试这个,

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

你可以发展自己的Separator类:

 public class SimpleSeparator { 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("]"); } } 

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

试试这个

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