public Image addComments(String createComments) {
comments.append(createComments ",");
return this;
}
這就是我得到的,A,B,
這就是我想要的樣子:A,B
我嘗試使用正則運算式
createComments = createComments.replaceAll(",$", "");
但它沒有用。
uj5u.com熱心網友回復:
如果comments是StringBuilder(或StringBuffer),您可以避免添加尾隨逗號:
public Image addComments(String createComments) {
if (comments.length() > 0) comments.append(",");
comments.append(createComments);
return this;
}
uj5u.com熱心網友回復:
我認為您可以使用任何 Collection 和 String.join 來避免整個逗號情況
Collection<String> comments = Arrays.asList("A", "B", "C", "D");
final String joinedComments = String.join(",", comments);
System.out.println(joinedComments);
這將回傳
A,B,C,D
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/341671.html
