使用 StringBuilder 时从 last 中删除逗号
Removed comma from last while working with StringBuilder
我有以下 Java 程序,但我不想在最后一个元素之后分配“,”,怎么办?
String range = "400-450";
Integer startRange = null;
Integer endRange = null;
StringTokenizer st = new StringTokenizer(range,"-");
while(st.hasMoreTokens()) {
startRange = Integer.parseInt(st.nextToken());
endRange= Integer.parseInt(st.nextToken());
}
StringBuilder sb = new StringBuilder();
for (int i = startRange; i <= endRange; i++) {
sb.append(i).append(",");
}
System.out.println(sb);
输出应该是
400,401,402,403,404,405,406,407,408,409,410,411,412,413,414,415,416,417,418,419,420,421,422,423,424,425,426,427,428,429,430,431,432,433,434,435,436,437,438,439,440,441,442,443,444,445,446,447,448,449,450
--> 最后没有逗号
对于 Java 8,您可以切换到新的 class StringJoiner
,它正是为 目的而添加的:
StringJoiner sb = new StringJoiner(",");
for (int i = startRange; i <= endRange; i++) {
sb.add(String.valueOf(i));
}
System.out.println(sb.toString());
手动执行,我建议在项目 之前添加逗号 然后使用 substring(1):
for (int i = startRange; i <= endRange; i++) {
sb.append(",").append(i);
}
//Check for empty before!
System.out.println(sb.substring(1));
让最后一个附加逗号的循环在到达最后一个元素之前停止:
String range = "400-450";
Integer startRange = null;
Integer endRange = null;
StringTokenizer st = new StringTokenizer(range,"-");
while(st.hasMoreTokens()) {
startRange = Integer.parseInt(st.nextToken());
endRange= Integer.parseInt(st.nextToken());
}
StringBuilder sb = new StringBuilder();
// here, the "<=" was changed to "<"
for (int i = startRange; i < endRange; i++) {
sb.append(i).append(",");
}
// append last element
sb.append(endrange)
System.out.println(sb);
您在每个 i
之后附加 ","
尝试仅在未达到 endRange
时附加 ","
做:
for (int i = startRange; i <= endRange; i++) {
sb.append(i)
if (i != endRange)
sb.append(",");
}
这样","
不会在最后一个号码后添加到sb。
Java < 8.
使用 Joiner (guava)
List<Integer> numbers = Lists.newArrayList(1, 3, 4, 5, 23);
System.out.println(Joiner.on(",").join(numbers));
输出为:
1,3,4,5,23
就这么简单
for (int i = startRange; i <= endRange; i++) {
sb.append(i);
if(i != endRange)
sb.append(",");
}
如@Jan 所说,如果你有 Java8,请使用 StringJoiner
。否则,您可以在新条目之前添加一个分隔符,并通过将分隔符初始化为空白来区别对待第一项。
String separator = "";
for (int i = startRange; i <= endRange; i++) {
sb.append(separator).append(i);
separator = ",";
}
这是Java8版本
StringJoiner sb = new StringJoiner(",");
for (int i = startRange; i <= endRange; i++) {
sb.add(String.valueOf(i));
}
我有以下 Java 程序,但我不想在最后一个元素之后分配“,”,怎么办?
String range = "400-450";
Integer startRange = null;
Integer endRange = null;
StringTokenizer st = new StringTokenizer(range,"-");
while(st.hasMoreTokens()) {
startRange = Integer.parseInt(st.nextToken());
endRange= Integer.parseInt(st.nextToken());
}
StringBuilder sb = new StringBuilder();
for (int i = startRange; i <= endRange; i++) {
sb.append(i).append(",");
}
System.out.println(sb);
输出应该是
400,401,402,403,404,405,406,407,408,409,410,411,412,413,414,415,416,417,418,419,420,421,422,423,424,425,426,427,428,429,430,431,432,433,434,435,436,437,438,439,440,441,442,443,444,445,446,447,448,449,450
--> 最后没有逗号
对于 Java 8,您可以切换到新的 class StringJoiner
,它正是为 目的而添加的:
StringJoiner sb = new StringJoiner(",");
for (int i = startRange; i <= endRange; i++) {
sb.add(String.valueOf(i));
}
System.out.println(sb.toString());
手动执行,我建议在项目 之前添加逗号 然后使用 substring(1):
for (int i = startRange; i <= endRange; i++) {
sb.append(",").append(i);
}
//Check for empty before!
System.out.println(sb.substring(1));
让最后一个附加逗号的循环在到达最后一个元素之前停止:
String range = "400-450";
Integer startRange = null;
Integer endRange = null;
StringTokenizer st = new StringTokenizer(range,"-");
while(st.hasMoreTokens()) {
startRange = Integer.parseInt(st.nextToken());
endRange= Integer.parseInt(st.nextToken());
}
StringBuilder sb = new StringBuilder();
// here, the "<=" was changed to "<"
for (int i = startRange; i < endRange; i++) {
sb.append(i).append(",");
}
// append last element
sb.append(endrange)
System.out.println(sb);
您在每个 i
之后附加 ","
尝试仅在未达到 endRange
","
做:
for (int i = startRange; i <= endRange; i++) {
sb.append(i)
if (i != endRange)
sb.append(",");
}
这样","
不会在最后一个号码后添加到sb。
Java < 8.
使用 Joiner (guava)List<Integer> numbers = Lists.newArrayList(1, 3, 4, 5, 23);
System.out.println(Joiner.on(",").join(numbers));
输出为:
1,3,4,5,23
就这么简单
for (int i = startRange; i <= endRange; i++) {
sb.append(i);
if(i != endRange)
sb.append(",");
}
如@Jan 所说,如果你有 Java8,请使用 StringJoiner
。否则,您可以在新条目之前添加一个分隔符,并通过将分隔符初始化为空白来区别对待第一项。
String separator = "";
for (int i = startRange; i <= endRange; i++) {
sb.append(separator).append(i);
separator = ",";
}
这是Java8版本
StringJoiner sb = new StringJoiner(",");
for (int i = startRange; i <= endRange; i++) {
sb.add(String.valueOf(i));
}