public class Main {
public static void main(String[] args) {
String[] words = {"The", "quick", "brown", "fox", ",", "jumps", "over", "the", "lazy", "dog", "."};
String concatenatedString = "";
for (String word : words) {
concatenatedString += word + " ";
}
System.out.println(concatenatedString);
}}
我尝试将字符串数组中的所有单词连接到一个字符串中,得到的输出如下:“敏捷的棕色狐狸,跳过懒狗。”
我希望删除标点符号前的空格,这样输出就可以像这样:“敏捷的棕色狐狸,跳过懒狗。”你知道我该怎么做吗?
发布于 2021-09-15 14:05:42
当您将字符与空格连接在一起时,可以对字符串变量进行替换,以删除不需要的空格
concatenatedString = concatenatedString.replaceAll(" ,", ",")
.replaceAll(" \\.", ".");https://stackoverflow.com/questions/69194588
复制相似问题