ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

2021-10-06

2021-10-06 23:00:48  阅读:126  来源: 互联网

标签:10 s8 06 String System 2021 println Hello out


话不多说,直接上干货,后续会出更多小知识!

第一章:String类中常用的成员方法

1.String的常见方法

concat: 拼接字符串,功能和字符串的+操作类似
contains: 判断是否包含
startsWith: 判断是否以什么开头的     
endsWith: 判断是否以什么结尾的      
indexOf:  查找第一次出现的索引
lastIndexOf: 查找最后一次出现的索引
replace: 替换
substring: 截取
toCharArray: 转成字符数组
split: 分割/切割字符串
trim: 去两端空格
toLowerCase: 转成小写
toUpperCase: 转成大写

2.常见方法演示

public class TestString {
    public static void main(String[] args) {
        //concat: 拼接字符串,功能和字符串的+操作类似
        String s1 = "Hello";
        s1 = s1.concat("World");
        System.out.println(s1);
        //思考: concat 和  + 有什么区别
        //a.concat只能用于2个字符串拼接, 而+可以拼接字符串和任意的其他类型
        //b.性能上来看:concat性能略高于+

        //contains: 判断是否包含
        String s2 = "HelloJavaWorld";
        System.out.println(s2.contains("Java"));

        //startsWith: 判断是否以什么开头的
        String s3 = "HelloJavaWorld";
        System.out.println(s3.startsWith("Hel")); //判断姓
        //endsWith: 判断是否以什么结尾的
        String s4 = "HelloJavaWorld";
        System.out.println(s4.endsWith("rld")); // 判断文件的格式

        //indexOf:  查找第一次出现的索引
        String s5 = "HelloJavaHelloWorld";
        System.out.println(s5.indexOf("Hello"));
        //lastIndexOf: 查找最后一次出现的索引
        String s6 = "HelloJavaHelloWorld";
        System.out.println(s6.lastIndexOf("Hello"));

        //replace: 替换
        String s7 = "Hello程序员Hello程序员";
        s7 = s7.replace("程序员","***");
        System.out.println(s7);

        //substring: 截取
        String s8 = "HelloJavaWorld";
        //s8 = s8.substring(5, 9); //含头不含尾
        //System.out.println(s8);
        s8 = s8.substring(5); //从指定索引开始截取直到字符串结束
        System.out.println(s8);

        //toCharArray: 转成字符数组
        String s9 = "Hello";
        char[] chs = s9.toCharArray();
        System.out.println(Arrays.toString(chs));

        //split: 分割/切割字符串
        String s10 = "0550-3004-3334-6468";
        String[] nums = s10.split("-");
        System.out.println(Arrays.toString(nums));

        //trim: 去两端两端两端空格
        String s11 = "   Hello  World  ";
        System.out.println("--->"+s11+"<---");
        s11 = s11.trim();
        System.out.println("--->"+s11+"<---");

        //toLowerCase: 转成小写
        String s12 = "HelloWorld";
        s12 = s12.toLowerCase();
        System.out.println(s12);
        //toUpperCase: 转成大写
        String s13 = "HelloWorld";
        s13 = s13.toUpperCase();
        System.out.println(s13);
    }
}

标签:10,s8,06,String,System,2021,println,Hello,out
来源: https://blog.csdn.net/qq_45260054/article/details/120630091

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有