我目前正在学习如何操作字符串,我认为我需要一段时间才能习惯它。我想知道如何将每个句子中句号后的字母大写。
输出是这样的:
输入句子:我很高兴。这是天才。
大写:我很高兴。这是天才。
我尝试创建自己的代码,但它不起作用,请随意更正和更改它。这是我的代码:
package Test;
import java.util.Scanner;
public class TestMain {
public static void main(String[]args) {
String sentence = getSentence();
int position = sentence.indexOf(".");
while (position != -1) {
position = sentence.indexOf(".", position + 1);
sentence = Character.toUpperCase(sentence.charAt(position)) + sentence.substring(position + 1);
System.out.println("Capitalized: " + sentence);
}
}
public static String getSentence() {
Scanner hold = new Scanner(System.in);
String sent;
System.out.print("Enter sentences:");
sent = hold.nextLine();
return sent;
}
}
Run Code Online (Sandbox Code Playgroud)
棘手的部分是我如何将句号(“.”)后的字母大写?我没有太多的字符串操作知识,所以我真的陷入了这个领域。