Common methods of P471String class - common methods of P477StringBuffer

Common methods of P471String class - common methods of P477StringBuffer

Common methods of P471String class

// 4.indexOf gets the index of the first occurrence of the character in the string object. The index starts from 0. If it is not found, it returns - 1

String s1 = "wer@terwe@g"; int index = s1.indexOf('@'); int index = s1.indexOf("we");

// 6.substring intercepts the substring of the specified range

String name = "hello, Zhang San";

//Below is name Substring (6) intercepts all subsequent contents from index 6//

System.out.println(name.substring(6));// Output Zhang San

//name.substring(0,5) means to intercept from index 0, intercept 5-0 indexes, and output hello

P472 substring()

System.out.println(name.substring(2,5));// Output llo, starting from index 2, and find 5-2 = 3 from 2

P472java String method 2

// 1.toUpperCase to uppercase

String s = "heLLo";

System.out.println(s.toUpperCase());//HELLO

// 3.concat splicing String s1 = "Baoyu";

s1 = s1.concat("Lin Daiyu") Concat ("Xue Baochai") concat("together");

System.out.println(s1);// Baoyu Lin Daiyu Xue Baochai together

// 4.replace replaces the character s1 = "Baoyu and Lin Daiyu, Lin Daiyu, Lin Daiyu" in the string// In s1, replace all Lin Daiyu with Xue Baochai

//Lao Han's interpretation: S1 After the replace () method is executed, the returned result is replaced

//Note that there is no effect on s1

String s11 = s1.replace("Baoyu", "jack");

System.out.println(s1);// Baoyu and Lin Daiyu Lin Daiyu Lin Daiyu

System. out. println(s11);// Lin Daiyu and Lin Daiyu

// 5.split splits a string. For some split characters, we need to escape, such as | \

String poem = "at noon on the weeding day, sweat drops into the soil. Who knows that every grain of Chinese food is hard";

//Lao Han's interpretation:

// 1. Split the poem based on, and return an array

// 2. When splitting a string, if there are special characters, you need to add an escape character\

String[] split = poem.split(","); // The output is {weeding day at noon, sweat drops into the soil, but the Chinese food is hard}

poem = "E:\aaa\bbb"; String []split = poem.split("\"); Output "E:\ \ aaa\ \ bbb". Note that if it was originally \ \, use 4\

// 6.toCharArray is converted to character array s = "happy"; char[] chs = s.toCharArray();

// 7.compareTo compares the size of two strings. If the former is large, it returns a positive number, and if the latter is large, it returns a negative number. If they are equal, it returns 0. Note that the A-65 and a-97 source codes actually compare ASCII codes

 

For interpretation, first compare the length to find out the minimum length, and then compare each character v1[k] for comparison. If the minimum length is still not compared, return len1-len2 (provided that the comparison is equal). If the minimum length is not reached, it will be different. Let different c1-c2 get values and compare them with ASCII code

String a = "jcck";// len = 3

String b = "jack";// len = 4

System.out.println(a.compareTo(b)); // The return value is the value of 'c' - 'a' = 2

P473format

// 8.format string% s string% c character% d integer%. 2f floating point

//Lao Han's interpretation

//1. % S,% D,% 2F,% C are called placeholders / / 2 These placeholders are replaced by the following variables / / 3.% S means to be replaced by a string

//4. %d is an integer to replace / / 5.% 2F means to replace with decimal. After replacement, only two decimal places will be reserved and rounded

//6. %c replace with char type

String formatStr = "my name is% s, age is% d, grade is%. 2f, gender is% c. I hope you like me!";

String info2 = String.format(formatStr, name, age, score, gender); System.out.println("info2=" + info2);

P473StringBuffer

//Lao Han's interpretation / / 1 The immediate parent class of StringBuffer is AbstractStringBuilder
 // 2. StringBuffer implements Serializable, that is, the objects of StringBuffer can be serialized
 // 3.  AbstractStringBuilder has the attribute char[] value in the parent class, not final
 //The value array stores the string contents and leads out the values stored in the heap
 // 4. StringBuffer is a final class and cannot be inherited
 // 5.  Because the StringBuffer character content has char[] value, all are changing (adding / deleting)
//It is not necessary to change the address every time (that is, it is not necessary to create a new object every time), so the efficiency is higher than that of String

2) Understanding: StringBuffer value heap, when 16 spaces are full, it is expanded again, and the original is copied (similar to buffer)

P475StringBuffer constructor and String and StringBuffer are converted to each other

StringBuffer stringBuffer = new StringBuffer();
//2. Specify char [] size through constructor
StringBuffer stringBuffer1 = new StringBuffer(100);
//3. Create a StringBuffer for a String. At this time, the size of char [] is str.length()+16
StringBuffer hello = new StringBuffer("hello");
//String > StringBufer
String str = "hello tom"; 
//Method 1: use the constructor. / / Note: the returned StringBuffer object has no effect on str itself 
StringBuffer stringBuffer = new StringBuffer(str); 
//Method 2 uses the append method
StringBuffer s = new StringBuffer();
s = s.append(str);
​
//Look at StringBuffer - > string 
StringBuffer stringBuffer3 = new StringBuffer("Han Shunping Education"); 
//Method 1: use the toString method provided by StringBuffer
String s0 = stringBuffer3.toString(); 
//Method 2: use the constructor to do it 
String s1 = new String(stringBuffer3);

Common methods of P476JavaStringBuffer

StringBuffer s = new StringBuffer("hello");
 //increase
 s.append(',');// "hello," returns StringBuffer
 s.append("Zhang Sanfeng");//"hello, Zhang Sanfeng"
 s.append("Zhao Min").append(100).append(true).append(10.5);//
 System.out.println(s);//The output "hello, Zhang Sanfeng, Zhao Min 100true10.5" actually calls toString() of StringBuffer
 //Delete
  /*Delete characters at index > = start & & < end
   * Interpretation: delete characters 11 ~ 14 [11, 14) */
 s.delete(11, 14);
 System.out.println(s);// "hello, Zhang Sanfeng, Zhao Min, true10.5"
 // change
 // Lao Han interprets and uses Zhou Zhiruo to replace the characters of index 9-11 [9,11)
 s.replace(9, 11, "Zhou Zhiruo");
 System.out.println(s);//"hello, Zhang Sanfeng, Zhou Zhiruo, true10.5"
 // check
 // Finds the index of the specified substring at the first occurrence of the string. If it is not found, it returns - 1
  int indexOf = s.indexOf("Zhang Sanfeng");
  System.out.println(indexOf);// Output 6
 //insert
 //Lao Han interpreted that "Zhao Min" was inserted at the position with index 9, and the content with index 9 was automatically moved back
s.insert(9, "Zhao Min");
System.out.println(s);// "hello, Zhang Sanfeng, Zhao Min, Zhou Zhiruo, true10.5"
 // length
 System.out.println(s.length());// 22

477 test questions

String str = null;// ok
StringBuffer sb = new StringBuffer(); //ok
sb.append(str);//You need to look at the source code,
// The bottom layer calls the appendNull of AbstractStringBuilder. If it is idle, it will be value={'n',..}
System.out.println(sb.length());//4
System.out.println(sb);//null
// The following constructor will throw NullpointerException
StringBuffer sb1 = new StringBuffer(str);
//Look at the underlying source code super(str.length() + 16)// What we pass in is null, and the null pointer exception is thrown
System.out.println(sb1);

Keywords: Java Back-end intellij-idea

Added by soulrazer on Tue, 18 Jan 2022 06:41:37 +0200