gpt4 book ai didi

java - 循环后如何将这些单词构建为一个句子?

转载 作者:行者123 更新时间:2023-12-01 19:33:29 27 4
gpt4 key购买 nike

我循环遍历一个句子,将其拆分为大写。但是在获取单个单词后很难将其重新构建。

String str = "Not the answer you're looking for.";
StringBuilder stringBuilder = new StringBuilder();
String oneWord =" ";
for (String word : str.toLowerCase().split(" ")){
char firstLetter = word.substring(0,1).toUpperCase().charAt(0);
oneWord = firstLetter + word.substring(1);

System.out.println(stringBuilder.append(oneWord + " "));

}

}

我希望只得到一个完全构建的字符串“不是您正在寻找的答案。”

最佳答案

  String str = "Not the answer you're looking for.";
StringBuilder stringBuilder = new StringBuilder();
String oneWord =" ";
for (String word : str.toLowerCase().split(" ")){
char firstLetter = word.substring(0,1).toUpperCase().charAt(0);
oneWord = firstLetter + word.substring(1);
stringBuilder.append(oneWord + " ");
}
System.out.println(stringBuilder.toString());

您不会只得到一个字符串,因为您在 for 循环中使用了 System.out.println

考虑我上面的例子

关于java - 循环后如何将这些单词构建为一个句子?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58645375/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com