11.20 Daily

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

public class day16_01 { public static void main(String[]args){ String a="The moonlight in front of the bed, suspected to be frost on the ground, raise your head to look at the moon, bow your head to think of hometown"; /verse

String[] b = a.split(",");      /用逗号分割诗句,字符串会变成多个字符串,所以要用数组接收

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

}
1
2
3
4
5
6
7
}
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"); /Write a string Buff class, named con, the value is hello System.out.println(con); /Printout } } 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"); /Write a string Buff class, named con, and the value is hello con.append("java"); /Add java System.out.println(con); /Print output } } 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,"..."); /In subscript 1, insert "..." System.out. println(con); /Print output } }





Guess you like

Origin blog.csdn.net/zzxin1216/article/details/110037975