package com.Test;
public class ControlString {
public void First_toUpper(){
String S="let there be light";
String[] s1=S.split(" ");
for(int i=0;i<s1.length;i++){
String s2=s1[i].substring(0,1).toUpperCase();
String s3=s1[i].substring(1);
s3=s2+s3;
System.out.print(s3+" ");
}
System.out.println();
}
public void count_same_p(){
int count=0;
String s1="peter piper picked a peck of pickled peppers";
String[] s2=s1.split(" ");
for(int i=0;i<s2.length;i++){
String s3=s2[i].substring(0,1);
if(s3.equals("p")){
count++;
}
}
System.out.println("以p开头的单词共有"+count+"个");
}
public void Low_toUpper(){
String s="lengendary";
char[] s1=s.toCharArray();
for(int i=0;i<s1.length;i+=2){
s1[i]=Character.toUpperCase(s1[i]);
}
String s2=String.valueOf(s1);
System.out.println("转换之后的字符串为:"+s2);
}
public void replace_last() {
String s = "Nature has given us that two ears, two eyes, and but one tongue, to the end that we should hear and see more than we speak";
System.out.println(s);
String[] s1 = s.split(" ");
for (int i = s1.length - 1; i > 0; i--) {
if (s1[i].equals("two")) {
String s2 = s1[i].substring(0,1).toUpperCase();
String s3 = s1[i].substring(1);
s1[i] = s2 + s3;
break;
}
}
for (String s4 : s1) {
System.out.print(s4 + " ");
}
}
public static void main(String[] args){
ControlString c1=new ControlString();
c1.First_toUpper();
c1.count_same_p();
c1.Low_toUpper();
c1.replace_last();
}
}
|