44 java常用类_6 _String案例演示

44 java常用类_6 _String案例演示

案例演示

需求:

  1. 已知String str = “this is a text” ;
  2. 将str中的单词单独获取出来
  3. 将str中的text替换为practice
  4. 在text前面插入一个easy
  5. 将每个单词的首字母改为大写

代码如下:

package com.wlw.common_class.string;

import java.util.Arrays;

public class Demo03 {
/*
需求:

1. 已知String str = "this is a text" ;
2. 将str中的单词单独获取出来
3. 将str中的text替换为practice
4. 在text前面插入一个easy
5. 将每个单词的首字母改为大写
 */
    public static void main(String[] args) {
        String str = "this is a text";

        System.out.println("---------------1.将str中的单词单独获取出来----------------------");
        String[] strings = str.split(" ");

        for (String string : strings) {
            System.out.println(string);
        }

        System.out.println("---------------2.将str中的text替换为practice----------------------");
        String replace = str.replace("text", "practice ");
        System.out.println(replace);

        System.out.println("---------------3.在text前面插入一个easy----------------------");
        String replace1 = str.replace("text", "easy text");
        System.out.println(replace1);

        System.out.println("---------------4.将每个单词的首字母改为大写----------------------");
        for (int i = 0; i <strings.length ; i++) { //遍历数组

            char c = strings[i].charAt(0); // 获得每一个字符串的首字符
            char newC = Character.toUpperCase(c);//变大写

            String newStr = newC + strings[i].substring(1);//合并字符串
            System.out.println(newStr);

        }
    }
}
/*执行结果:
---------------1.将str中的单词单独获取出来----------------------
this
is
a
text
---------------2.将str中的text替换为practice----------------------
this is a practice 
---------------3.在text前面插入一个easy----------------------
this is a easy text
---------------4.将每个单词的首字母改为大写----------------------
This
Is
A
Text

*/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

悬浮海

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值