public class Demo {
public String firstLetterString(String s){
//将完整的字符串按照空格拆分 split
//循环处理每一个单词,截取首字母->大写,其余的在拼接上去
//每一次的单词拼接成完整的字符串
String result = " ";
String[] value = s.split(" ");
for(int i = 0; i < value.length; i++){
String firstLetter = value[i].substring(0,1).toUpperCase();
String nextLetter = value[i].substring(1);
result = result.concat(firstLetter.concat(nextLetter) + " ");
}
return result.trim();//去掉字符串末尾多余的空格
}
public static void main(String[] args){
Demo demo = new Demo();
System.out.println("请输入字符串:");
Scanner scanner = new Scanner(System.in);
String str = scanner.nextLine();
str = demo.firstLetterString(str);
System.out.println(str);
}
}
public class work2 {
public static void main(String[] args){
System.out.println("请输入字符串:");
Scanner scanner = new Scanner(System.in);
String str= scanner.nextLine();
int n = 0;
do {
String c1 = str.substring(n,n+1);//截取首部字母
String c2 = c1.toUpperCase();//c2是首部字符变为大写
if (n == 0){
str = str.replace(c1, c2);
}else {
str = str.replace(" "+c1, " "+c2);
}
int n1 = str.indexOf(' ',n);//找到空格的位置let there字符串之间的空格位置
//找到下一个首字母,indexOf找不到时返回-1
n = n1+1;
}while (n != 0);
System.out.println(str);
}
}
单纯记录一下初学java遇到的问题,方便以后复习