Java is in hand, I have the world! ! ! Java basics-String class

table of Contents

What is String

Characteristics of strings

 Common 3+1 methods for creating strings

== Usage notes in String

Common methods in String

 1. How to use equals

 2. Common methods related to obtaining in String are

 3. The interception method of string

 4. Common methods related to conversion in String are

 5. Method of dividing a string


What is String

The java.lang.String class represents a string
The API says: All string literals (such as "abc") in Java programs are implemented as instances of this class. It means: all double-quoted strings in the program are objects of the String class (even if there is no new, they are still).
 

Characteristics of strings

1. The content of the string is never changeable, it is a constant [emphasis]
2. It is because the string cannot be changed, so the string can be shared and used
3. String is equivalent to char[] character array in effect, but the underlying principle is byte[] byte array
 

Common 3+1 methods for creating strings

三种构造方法:
public String():创建一个空白字符串,不含有任何内容
public String(char[] array):根据字符数组的内容,来创建对应的字符串。
public String(byte[] array):根据字节数组的内容,来创建对应的字符串。
一种直接创建:
String str="Hello";     //右边直接用双引号
注意:直接写上双引号,就是字符串对象。(默认已经new了)
public class Demo01String {
    public static void main(String[] args) {
        //使用空参构造,小括号留空,说明字符串什么内容都没有
        String str1=new String();       
        System.out.println("第1个字符串:"+str1);
        //根据字符数组创建字符串
        char[] charArray={'A','B','C'};
        String str2=new String(charArray);
        System.out.println("第2个字符串:"+str2);
        //根据字节数组创建字符串
        byte[] byteArray={97,98,99};
        String str3=new String(byteArray);
        System.out.println("第3个字符串:"+str3);
        //直接创建
        String str4="Hello";
        System.out.println("第4个字符串:"+str4);
    }
}

== Usage notes in String

    字符串常量池:程序当中直接写上的双引号字符串,就在字符串常量池当中。new的不在池当中。
    对于基本类型来说,==进行的是数值的比较,
    对于引用类型来说,==是进行【地址值】的比较。
public class Demo02String {
    public static void main(String[] args) {
        String str1="abc";
        String str2="abc";
        char[] charArray={'a','b','c'};
        String str3=new String(charArray);

        System.out.println(str1==str2);     //true
        System.out.println(str1==str3);     //false
        System.out.println(str3==str2);     //false
    }
}

Common methods in String

 1. How to use equals

==是对象进行的地址值比较,如果确实需要字符串的内容进行比较,可以使用两个方法
第一种方法:
public boolean equals(Object obj):参数可以是任何对象,只有参数是一个字符串并且内容相同的时候才会给true,否则返回false
注意事项:
1.任何对象都能用Object进行接受
2.equals方法具有对称性,也就是a.equals(b)和b.equals(a)效果一样
3.如果比较双方一个常量一个变量,推荐将常量字符串写在前面
推荐:"abc".equals(str)   不推荐:str.equals("abc")
第二种方法:
public boolean equalsIgnoreCase(String str):忽略大小写,进行内容比较。
public class Demo03StringEquals {
    public static void main(String[] args) {
        String str1="Hello";
        String str2="Hello";
        char[] charArray={'H','e','l','l','o'};
        String str3=new String(charArray);

        System.out.println(str1.equals(str2));      //true
        System.out.println(str2.equals(str3));      //true
        System.out.println(str3.equals("Hello"));   //true
        System.out.println("Hello".equals(str3));   //true

        String str4="hello";
        System.out.println(str1.equals(str4));      //false
        System.out.println("*****************************************");

        String str5="abc";
        System.out.println("abc".equals(str5));     //推荐写法
        System.out.println(str5.equals("abc"));     //不推荐

        String str6=null;
        System.out.println("abc".equals(str6));     //推荐写法
        //System.out.println(str6.equals("abc"));     //不推荐,报错  NullPointerException空指针异常

        System.out.println("****************************************");
        String str7="ABC";
        String str8="abc";
        System.out.println(str7.equals(str8));      //false
        //不区分大小写的比较
        System.out.println(str7.equalsIgnoreCase(str8));    //true
    }
}

2. Common methods related to obtaining in String are

public int length():获取字符串当中含有的字符个数,拿到字符串长度。
public String concat(String str):将当前字符串和参数字符串拼接成为返回值新的字符串
public char charAt(int index):获取指定索引位置的单个字符。(从索引0开始)
public int indexOf(String str):查找参数字符串在本字符串当中首次出现的索引位置,如果没有返回-1值 
public class Demo04StringGet {
    public static void main(String[] args) {

        //获取字符串的长度
        int length="jfkdjfkjdkfjkdsjfkasjfksd".length();
        System.out.println("字符串长度是:"+length);
        System.out.println("********************************");
        
        //拼接字符串
        String str1="Hello";
        String str2="World";
        String str3=str1.concat(str2);
        System.out.println(str1);   //Hello 原封不动
        System.out.println(str2);   //World 原封不动
        System.out.println(str3);   //HelloWorld  新字符串
        System.out.println("********************************");

        //获取指定索引位置的单个字符
        char ch="Hello".charAt(1);      
        System.out.println("第1号索引位置的字符是:"+ch);    //e
        System.out.println("********************************");

        //查找参数字符串在本来字符串当中第一次出现的索引位置
        //如果根本没有,返回-1
        String original="HelloWorldHelloWorldHelloWorld";
        int index=original.indexOf("llo");
        System.out.println("第一次索引值是:"+index);       //2
        int index1=original.indexOf("abc");
        System.out.println("第一次索引值是:"+index1);       //-1
    }
}

 3. The interception method of string

public String substring(int index):截取从参数位置一直到字符串末尾,返回新字符串
public String substring(int begin,int end):截取从begin开始,一直到end结束,中间的字符串。
备注:[begin.end):包含左边,不包含右边
public class Demo05Substring {
    public static void main(String[] args) {
        String str1="HelloWorld";
        String str2=str1.substring(5);
        System.out.println(str1);       //HelloWorld 原封不动
        System.out.println(str2);       //World 新字符串
        System.out.println("************************************");

        String str3=str1.substring(4,7);
        System.out.println(str3);       //oWo
        System.out.println("*************************************");
    }
}

 4. Common methods related to conversion in String are

public char[] toCharArray():将当前字符串拆分为字符数组作为返回值
public byte[] getBytes():获得当前字符串底层的字节数组
public String replace(CharSequence oldString,CharSequence newString):
将所有出现的老字符串替换成为新的字符串,返回替换之后的结果新字符串
public class Demo06StringConvert {
    public static void main(String[] args) {

        //转换成字符数组
        char[] chars="Hello".toCharArray();
        System.out.println(chars[0]);   //H
        System.out.println(chars.length);       //5
        System.out.println("*****************************");

        //转换成字节数组
        byte[] bytes="abc".getBytes();
        for (int i=0;i<bytes.length;i++){
            System.out.println(bytes[i]);        97  98 99
        }
        System.out.println("*****************************");

        //字符串内容的替换
        String str1="How do you do?";
        String str2=str1.replace("o","*");
        System.out.println(str1);   //How do you do?
        System.out.println(str2);   //H*w d* y*u d*?
        System.out.println("*****************************");
    }
}

 5. Method of dividing a string

public String[] split(String regex):按照参数的规则,将字符串切分为若干个部分
注意事项:
split方法的参数其实是一个”正则表达式“
如果按照英语句点”.“进行切分,必须写成:"\\."(加两个反斜杠)
public class Demo07StringSplit {
    public static void main(String[] args) {
        String str1="aaa,bbb,ccc";
        String[] array1=str1.split(",");
        for (int i = 0; i < array1.length; i++) {
            System.out.println(array1[i]);
        }
        System.out.println("*******************************");
    }
}

 

Guess you like

Origin blog.csdn.net/wtt15100/article/details/108045469