String类中的一些知识及方法

1)String的一些知识点

  • 字符串一旦创建就不可变
  • 双引号括起来的字符串存储在字符串常量池中
  • 字符串的比较必须使用equals方法
  • String已经重写了toString()和equals()方法

2)String的构造方法

  • String s=" ";
  • String s=new String(" ");
  • String s=new String(byte数组);
  • String s=new String(byte数组,起始下标,长度);
  • String s=new String(char数组);
  • String s=new String(char数组,起始下标,长度);

3)String类常用的21个方法

            // String类当中常用方法。
            //1(掌握).char(返回类型)  charAt(int index)
            char c = "中国人".charAt(1);
            System.out.println(c); // 国

            // 2(了解).int compareTo(String anotherString)
            // 字符串之间比较大小不能直接使用 > < ,需要使用compareTo方法。
            int result = "abc".compareTo("abc");
            System.out.println(result); //0(等于0) 前后一致
            // 拿着字符串第一个字母和后面字符串的第一个字母比较。能分胜负就不再比较了。
            int result2 = "abcd".compareTo("abce");
            System.out.println(result2); //-1(小于0) 前小后大

            // 3(掌握).boolean contains(CharSequence s)
            // 判断前面的字符串中是否包含后面的子字符串。
            System.out.println("HelloWorld".contains("Hello")); // true

            // 4(掌握). boolean endsWith(String suffix)
            // 判断当前字符串是否以某个子字符串结尾。
            System.out.println("test.txt".endsWith(".java")); // false

            // 5(掌握).boolean equals(Object anObject)
            // 比较两个字符串必须使用equals方法,不能使用“==”
            // JDK13中并没有调用compareTo()方法。
            // equals只能看出相等不相等。
            // compareTo方法可以看出是否相等,并且同时还可以看出谁大谁小。
            System.out.println("abc".equals("abc")); // true

            // 6(掌握).boolean equalsIgnoreCase(String anotherString)
            // 判断两个字符串是否相等,并且同时忽略大小写。
            System.out.println("ABc".equalsIgnoreCase("abC")); // true

            // 7(掌握).byte[] getBytes()
            // 将字符串对象转换成字节数组
            byte[] bytes = "abcdef".getBytes();
            for(int i = 0; i < bytes.length; i++){
                System.out.println(bytes[i]);
            }

            // 8(掌握).int indexOf(String str)
            // 判断某个子字符串在当前字符串中第一次出现处的索引(下标)。
            System.out.println("oraclejavac++.netc#phppythonjavaoraclec++".indexOf("java")); // 6

            // 9(掌握).boolean isEmpty()
            // 判断某个字符串是否为“空字符串”。底层源代码调用的是字符串的length()方法。
            String s = "a";
            System.out.println(s.isEmpty());//false

            // 10(掌握). int length()
            // 判断数组长度和判断字符串长度不一样
            // 判断数组长度是length属性,判断字符串长度是length()方法。
            System.out.println("abc".length()); // 3

            // 11(掌握).int lastIndexOf(String str)
            // 判断某个子字符串在当前字符串中最后一次出现的索引(下标)
            System.out.println("oraclejavac++javac#phpjavapython".lastIndexOf("java")); //22

            // 12(掌握). String replace(CharSequence target, CharSequence replacement)
            // 替换。
            String newString2 = "name=zhangsan&password=123&age=20".replace("=", ":");
            System.out.println(newString2); //name:zhangsan&password:123&age:20

            // 13(掌握).String[] split(String regex)
            // 拆分字符串
            String[] ymd = "1980-10-11".split("-"); //"1980-10-11"以"-"分隔符进行拆分。
            for(int i = 0; i < ymd.length; i++){
                System.out.println(ymd[i]);
            }

            // 14(掌握)、boolean startsWith(String prefix)
            // 判断某个字符串是否以某个子字符串开始。
            System.out.println("HelloWorld".startsWith("World")); // false


            // 15(掌握)、 String substring(int beginIndex) 参数是起始下标。
            // 截取字符串
            System.out.println("http://www.baidu.com".substring(7)); //www.baidu.com

            // 16(掌握)、String substring(int beginIndex, int endIndex)
            // beginIndex起始位置(包括)
            // endIndex结束位置(不包括)
            System.out.println("http://www.baidu.com".substring(7, 10)); //www

            // 17(掌握)、char[] toCharArray()
            // 将字符串转换成char数组
            char[] chars = "我是中国人".toCharArray();
            for(int i = 0; i < chars.length; i++){
                System.out.println(chars[i]);
            }

            // 18(掌握)、String toLowerCase()
            // 转换为小写。
            System.out.println("ABCDefKXyz".toLowerCase());

            // 19(掌握)、String toUpperCase();
           //转换为大写
            System.out.println("ABCDefKXyz".toUpperCase());

            // 20(掌握). String trim();
            // 去除字符串前后空白
            System.out.println("           hello      world             ".trim());

            // 21(掌握). String中只有一个方法是静态的,不需要new对象
            // 这个方法叫做valueOf
            // 作用:将“非字符串”转换成“字符串”
            String s1 = String.valueOf(true);
            System.out.println(s1);
            // 这个静态的valueOf()方法,参数是一个对象的时候,会自动调用该对象的toString()方法

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值