String案例演示

public class Application   {
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
}
}
}
posted @   惊鸿宴远赴人间  阅读(10)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
点击右上角即可分享
微信分享提示