2020-11-24

Java

Splitting the string
//1. The method of splitting the string: split()
Usage: string.split ("split basis");
test: split verse

public class day16_01 {
    
    
    public static void main(String[]args){
    
    
        String a="床前明月光,疑似地上霜,举头望明月,低头思故乡";   /诗句
        
        String[] b = a.split(",");      /用逗号分割诗句,字符串会变成多个字符串,所以要用数组接收

		/用遍历,打印输出
		for(int i=0;i<b.length;i++){
    
    
            System.out.println(b[i]);
        }
    }
}

Insert picture description here

I came to the conclusion: After the division, the division basis will disappear, and the strings on the left and right sides will be divided into two, which need to be received by the array.
I wrote an article about arrays before. If you don’t understand, you can take a look:
array declaration and assignment Array traversal

String Buffer type of string: Handling frequently changed string variables.
If you frequently change string variables, it will open up new space, and then throw away the original space, which is very wasteful and takes up loading time, but StringBuffer will not, it will Always in one space

//1. Declare the format of assignment
StringBuffer con=new StringBuffer("content");
experiment:

public class day16_07 {
    
    
    public static void main(String[]args){
    
    
        StringBuffer con=new StringBuffer("hello");        /写一个字符串Buff类,命名为con,值是hello
        System.out.println(con);                           /打印输出
    }
}

Insert picture description here

//2. The method of adding strings: append() will not open up new space.
Usage: StringBuffer method.append("string");
Strings will be added later.
Test:

public class day16_07 {
    
    
    public static void main(String[]args){
    
    
        StringBuffer con=new StringBuffer("hello");        /写一个字符串Buff类,命名为con,值是hello
        con.append("java");              /添加java
        System.out.println(con);         /打印输出
    }
}

Insert picture description here

The method of inserting content: insert() will not open up new space.
Usage: StringBuffer method.insert (inserted index value, inserted content);
test:

public class day16_07 {
    
    
    public static void main(String[]args){
    
    
        StringBuffer con=new StringBuffer("hellojava");
        con.insert(1,"...");              /在下标1,插入"..."
        System.out.println(con);          /打印输出
    }
}

Guess you like

Origin blog.csdn.net/weixin_51734251/article/details/110070264