Java中String类常用方法

前言

String类中有很多我们未来要用到的方法,这里先介绍20个常用的方法

只要勤加练习,就能掌握

如下


代码

public class CeShi {
    public static void main(String[] args) {
        String s = new String("sss");
        System.out.println(s.toString());
        String s1 = "hello";
        String s2 = "hello";
        System.out.println(s1==s2);
        String x = new String("hello");
        String y = new String("hello");
        System.out.println(x==y);
        System.out.println(x.equals(y));
        String k=new String("test");
        System.out.println("test".length());
        System.out.println("test".equals(k));
        k = null;
        //System.out.println(k.equals("test"));
        byte[] bytes = {97,98,99};
        String s3 = new String(bytes);

        System.out.println(s3);
        String s4 = new String(bytes,1,2);
        System.out.println(s4);
        char[] s5 = {'我','是','中','国','人'};
        String s6 = new String(s5,2,3);
        System.out.println(s6);
        char c = "中国人".charAt(1);
        System.out.println(c);
        System.out.println("HelloWorld java".contains("java"));
        System.out.println("http".contains("tt3"));
        System.out.println("test.txt".endsWith(".java"));
        System.out.println("text.txt".endsWith("txt"));
        System.out.println("abc".equals("abc"));
        System.out.println("ABc".equalsIgnoreCase("abC"));
        byte[] bs = "abcdef".getBytes();
        for(int i = 0;i<bs.length;i++){
            System.out.println(bs);
        }
        System.out.println("sjksajdkfjajjava;afjdajfk".indexOf("java"));
        String s7 ="a";
        String s8 = "";
        System.out.println(s7.isEmpty());
        System.out.println(s8.isEmpty());
        System.out.println("java".length());
        System.out.println("第11个");
        System.out.println("sjdfhaskhfjksdhfjsd".lastIndexOf("hf"));
        String newString = "http://www.baidu.com".replace("http://","https://");
        System.out.println(newString);
        String[] ymd = "1980-10-11".split("-");
        for(int i = 0;i< ymd.length;i++){
            System.out.println(ymd[i]);
        }
        System.out.println("http://www.baidu.com".startsWith("http"));
        System.out.println("http://www.baidu.com".startsWith("https"));
        System.out.println("http://www.baidu.com".substring(7));
        System.out.println("http://www.baidu.com".substring(7,10));
        char[] chars = "我是中国人".toCharArray();
        for(int i = 0;i< chars.length;i++){
            System.out.println(chars[i]);
        }
        System.out.println("ABCDefKCyz".toLowerCase());
        System.out.println("ABCDefKCyz".toUpperCase());
        System.out.println("         hello         world           ".trim());

    }
}

结果

sss
true
false
true
4
true
abc
bc
中国人
国
true
false
false
true
true
true
[B@34c45dca
[B@34c45dca
[B@34c45dca
[B@34c45dca
[B@34c45dca
[B@34c45dca
12
false
true
4
第11个
14
https://www.baidu.com
1980
10
11
true
false
www.baidu.com
www
我
是
中
国
人
abcdefkcyz
ABCDEFKCYZ
hello         world

Process finished with exit code 0
本站部分文章资源来源于互联网,仅供学习交流,如若要商用,请购买正版!
若不听劝告,网友造成出现一切后果,与本站本人无关
本站有些资源未经测试,请注意网络安全,本站不对下载的资源造成的后果负责
免责声明
作者:昼白
转载请注明来源:https://www.2bcnm.com/2024.htm
THE END
分享
二维码
打赏
海报
Java中String类常用方法
前言 String类中有很多我们未来要用到的方法,这里先介绍20个常用的方法 只要勤加练习,就能掌握 如下 代码 public class CeShi { public……
<<上一篇
下一篇>>