我们可以先把每个字符都分别用标识符分隔出来,如果这个字符前面是标识符,则这个字符大写。
列如 hello_java_hello ,可以用"_"将这3个单词拿出来,然后让其首字母大写。
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (scanner.hasNext()){
String str = scanner.nextLine();
String [] words = str.split("_"); //先将字符串按照"_"切割
String result = words[0];
for(int i =1;i<words.length;i++){
result += (char)(words[i].charAt(0)-'a'+'A'); //将分隔后的单词的首字母大写。
for(int j=1;j<words[i].length();j++){
result+=words[i].charAt(j); //最后在拼接起来
}
}
System.out.println(result);
}
}