Java学习笔记

java.lang.String
String表示字符串类型,属于引用数据类型,不属于基本数据类型

构造方法

1.动态创建:String s = new String("");
2.静态创建:String s = "";
3.String s = new String(char);
4.String s = new String(char, 起始下标,长度);
5.String s = new String(byte);
6.String s = new String(byte, 起始下标,长度);

示例程序

public class StringTest04 {
    public static void main(String[] args) {
        //a b c
        byte[] bytes = {97,98,99};
        System.out.println("String s = new String(byte)");
        String s2 = new String(bytes);
        System.out.println(s2);

        System.out.println("String s = new String(byte, 起始下标,长度)");
        String s3 = new String(bytes , 1, 2);
        System.out.println(s3);

        char[] chars = {'开' , '心', '啊'};
        System.out.println("String s = new String(char)");
        String s4 = new String(chars);
        System.out.println(s4);

        System.out.println("String s = new String(byte, 起始下标,长度)");
        String s5 = new String(chars, 0 , 2);
        System.out.println(s5);
    }
}

常用方法

public class StringTest05 {
    public static void main(String[] args) {
        //1.char charAt(int index)
        char c = "中国人".charAt(1);
        System.out.println(c);  //国

        //2(了解)int compareTo(String anotherString)
        //等价于 strcmp
        int result = "abc".compareTo("abc");
        System.out.println(result);   //0

        int result1 = "abcd".compareTo("abce");
        System.out.println(result1);	  //-1

        int result2 = "abc".compareTo("abc");
        System.out.println(result2);  //1


        //3.boolean contains(CharSequence s)
        //判断前面的字符串是否包含后面的子字符串
        System.out.println("HelloWorld.java".contains(".java"));  //true
        System.out.println("http://".contains("https"));     //false


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


        //5.boolean equalsIgnoreCase(String anotherString)
        // 判断两个字符串是否相等,并且同时忽略大小写
        System.out.println("abc".equalsIgnoreCase("Abc")); //true

        //6. byte[] getBytes()
        //将字符串对象转换成字节数组
        byte[] bytes = "abcdef".getBytes();
        for(int i = 0; i < bytes.length;i++)
            System.out.println(bytes[i]);   //97 98 99 100...

        //7.int indexOf(String str)
        //判断某个字符串在当前字符串中第一次出现处的索引(下标)
        System.out.println("oracleJava".indexOf("Java"));   //6


        //8.boolean isEmpty()
        //判断一个字符串是否为空
        String s8 = "";
        System.out.println(s8.isEmpty());  //true

        //9.int length()
        //判断数组长度是length属性,判断字符串长度是length()方法
        System.out.println(s8.length());   //0

        //10. int.lastIndexOf(String str)
        //判断某个字符串在当前字符串中最后一次出现处的索引(下标)
        System.out.println("oracleJavaJava".lastIndexOf("Java"));   //10

        //11.String replace(CharSequence target, CharSequence replacement)
        //String的父接口就是:CharSequence
        String newString = "http://www.baidu.com".replace("http","https");

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

        //13.boolean startsWith(String prefix)
        //判断某个字符串是否以某个子字符串开始
        System.out.println("http://www.baidu".startsWith("http://"));  //true


        //14. String substring(int beginIndex)
        //截取字符串,参数是起始下标
        System.out.println("oracleJavaJava".substring(6));  //JavaJava
        //String substring(int beginIndex, int endIndex)
        //截取字符串,左闭右开
        System.out.println("oracleJavaJava".substring(6,10));  //Java

        //15.char[] toCharArray()
        //将字符串转化成char数组
        char[] chars = "HelloWorld".toCharArray();
        for(int i = 0; i < chars.length;i++)
            System.out.println(chars[i]);   // H e l l o ....

        //16. String toLowerCase()
        //转化为小写
        System.out.println("ABcsdf".toLowerCase());    //abcsdf
        //String toUpperCase()                     
        //转化为大写
        System.out.println("ABcsdf".toUpperCase());    //ABCSDF

        //17.String trim()
        //去除字符串前后的空格
        System.out.println("   ABcsdf   ".trim());    //ABcsdf

        //18.String只有一个方法是静态的,不需要new对象
        //valueOf
        //作用:将非字符串转化成字符串
        //当参数是一个对象的时候,会自动调用该对象的toString方法
        String s1 = String.valueOf(3.1415);       //3.1415
        System.out.println(s1);
        System.out.println(new Customer());       //VIP用户

        //19.println的源码:对于引用类型,调用valueOf方法,而valueOf方法会自动调用toString方法
        //对于基本数据类型和String类型的引用(动态和静态创建的字符串对象都是通过引用指向的),println调用底层方法输出
        //对于其他引用数据类型,调用valueOf()方法,而valueOf方法会调用toSting方法;
        //为什么对于引用数据类型不直接调用toString()方法?
        //toString方法不包含空指针检验,但是valueOf包括
        String s = new String("x2");
        System.out.println(100);
        System.out.println(true);
        System.out.println(s);
        System.out.println(new Customer());

        //对于引用数据类型,不要加上.toString
        //因为toString方法中没有空指针检验,本质上说println对于引用数据类型自动调用toString方法说错误的
        Customer s2 = null;
        System.out.println(s2);
    }
}

class Customer{
    @Override
    public String toString() {
        return "VIP客户";
    }
}
原文地址:https://www.cnblogs.com/zy200128/p/12827544.html