Java 给定字符串实现相应功能

给定字符串“ wejdhf.siduiad yersdoi3j8d0 sdasdwe.ttt ”,实现以下功能:

1.去两侧空格
2.空格分隔字符串
3.截取两个w之间的字符串
4.查找yer是否在字符串中
5.获取字符串的长度
6.替换字符串中doi为ddd
public class ZiFuChuan {
    public static void main(String[] args) {
        String str = "  wejdhf.siduiad yersdoi3j8d0 sdasdwe.ttt ";

        //去两侧空格
        String s = str.trim();
        System.out.println("去两侧空格:" + s);
        System.out.println();

        //空格分隔字符串
        String[] arr = str.split("\\s+");
        System.out.println("空格分隔字符串: ");
        for (String ss : arr) {
            System.out.println(ss);
        }
        System.out.println();

        //截取两个w之间的字符串
        String w = str.substring(str.indexOf("w")+2, str.indexOf("we."));
        System.out.println("截取两个w之间的字符串:"+w);
        System.out.println();

        //查找yer是否在字符串中
        boolean y = str.contains("yer");
        if (y) {
            System.out.println("yer在字符串中");
            System.out.println();
        } else {
            System.out.println("yer不在字符串中");
            System.out.println();
        }

        //获取字符串的长度
        int l = str.length();
        System.out.println("字符串长度为:" + l);
        System.out.println();

        //替换字符串中doi为ddd
        str = str.replace("doi", "ddd");
        System.out.println("替换后字符串为:" + str);
    }

}

运行结果:

去两侧空格:wejdhf.siduiad yersdoi3j8d0 sdasdwe.ttt

空格分隔字符串: 

wejdhf.siduiad
yersdoi3j8d0
sdasdwe.ttt

截取两个w之间的字符串:jdhf.siduiad yersdoi3j8d0 sdasd

yer在字符串中

字符串长度为:42

替换后字符串为:  wejdhf.siduiad yersddd3j8d0 sdasdwe.ttt 
©️2020 CSDN 皮肤主题: 深蓝海洋 设计师:CSDN官方博客 返回首页