public class Application {标签:演示,String,text,System,案例,str,println,out From: https://www.cnblogs.com/123456dh/p/17122428.html
public static void main(String[] args) {
String str="this is a text";
//1 将str中的单词单独获取出来
String[] s = str.split(" ");
for (String a:
s) {
System.out.println(a);
}
/*
this
is
a
text
*/
//2 将str中的text替换为practice
System.out.println(str.replace("text","practice"));//this is a practice
//3 在text前面插入一个easy
System.out.println(str.replace("text","easy text"));//this is a easy text
//4 将str中每个单词的首字母改为大写
//System.out.println(str.replace("this is a text","This Is A Text"));//This Is A Text
for (int i = 0; i <s.length ; i++) {
//获取第一个字符
char c = s[i].charAt(0);
//将第一个字符改为大写
char c1 = Character.toUpperCase(c);
//在把每个单词拼接起来
String s1 = c1 + s[i].substring(1);
System.out.print(s1+" ");//This Is A Text
}
}
}