我已经搜索了几个小时来解决此类问题,但没有成功。
在Java中,我尝试获得这种输出:
1, 2, 3
(不使用 StringBuilder)
我得到的结果是:
1, 2, 3,
使用代码:
for(int i=1; i <= 3; i++){
String output = "";
output += i + ", ";
System.out.print(output);
}
p.s 我的代码没有使用任何数组。
最佳答案
从 Java 8+ 开始,这可以使用 join 方法来实现。
String.join(",", 列表)
关于java - 如何删除循环中字符串中的最后一个分隔符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39312962/