StringBuffer class - constructor, capacity and length of string buffer, add, delete, replace, reverse, intercept functions

StringBuffer class——construction method of StringBuffer class, two common methods of StringBuffer (capacity and length of string buffer), addition function of StringBuffer class , delete function of StringBuffer class , replacement function of StringBuffer class , inversion of StringBuffer class Function, interception function of StringBuffer class , and corresponding code demonstration


1. Thread safety (multi-threading explanation)

(1) Security -- Synchronization -- Data is safe.

(2) Insecure -- out of sync -- more efficient.

(3) The problem of safety and efficiency is a problem that always troubles us.

        Security: hospital website, bank website

        Efficiency: news sites, forums, etc.

2. StringBuffer class:

(1) The StringBuffer class is a thread-safe variable string.

(2) What is the difference between StringBuffer and String?   

A: The former is variable in length and content, while the latter is immutable; if the former is used for string splicing, it will not waste too many resources.

3. The construction method of StringBuffer:

(1) public StringBuffer(): No parameter constructor

(2) public StringBuffer(int capacity): String buffer object with specified capacity

(3) public StringBuffer(String str): A string buffer object specifying the content of the string

4. Two common methods of StringBuffer:

(1) public int capacity(): Returns the current capacity: theoretical value, the default is 16 characters.

(2) public int length(): Returns the length (number of characters): the actual value.

5. Example of the construction method of StringBuffer:

public class StringBufferDemo {
	public static void main(String[] args) {

                 // public StringBuffer(): no-argument constructor
		StringBuffer sb = new StringBuffer();
		System.out.println("sb:" + sb);
		System.out.println("sb.capacity():" + sb.capacity());    //16
		System.out.println("sb.length():" + sb.length());    //0
		System.out.println("--------------------------");

		// public StringBuffer(int capacity): String buffer object with specified capacity
		StringBuffer sb2 = new StringBuffer(50);
		System.out.println("sb2:" + sb2);
		System.out.println("sb2.capacity():" + sb2.capacity());    //50
		System.out.println("sb2.length():" + sb2.length());    //0
		System.out.println("--------------------------");

		// public StringBuffer(String str): The string buffer object specifying the content of the string
		StringBuffer sb3 = new StringBuffer("hello");
		System.out.println("sb3:" + sb3);    //"hello"
		System.out.println("sb3.capacity():" + sb3.capacity());    //21
		System.out.println("sb3.length():" + sb3.length());    //5
	}
}

6. Added functions of StringBuffer:

(1)public StringBuffer append(String str)

        You can add any type of data to a string buffer (append to the end) and return the string buffer itself.

(2)public StringBuffer insert(int offset,String str)

        Inserts data of any type into the string buffer at the specified position, and returns the string buffer itself.

7. Code demonstration of adding functions:

public class StringBufferDemo {
	public static void main(String[] args) {
		// create string buffer object
		StringBuffer sb = new StringBuffer();

		// public StringBuffer append(String str)
		// StringBuffer sb2 = sb.append("hello");
		// System.out.println("sb:" + sb);
		// System.out.println("sb2:" + sb2);
		// System.out.println(sb == sb2); // true

		// add data step by step
		// sb.append("hello");
		// sb.append(true);
		// sb.append(12);
		// sb.append(34.56);

		// chain programming
		sb.append("hello").append(true).append(12).append(34.56);
		System.out.println("sb:" + sb);	//hellotrue1224.56

		// public StringBuffer insert(int offset,String
		// str): Insert any type of data into the string buffer at the specified position, and return the string buffer itself
		sb.insert(5, "world");
		System.out.println("sb:" + sb);//helloworldtrue1224.56
	}
}

8. The delete function of StringBuffer:

(1)public StringBuffer deleteCharAt(int index)

        Deletes the character at the specified position and returns the string buffer object itself.

(2)public StringBuffer delete(int start, int end)

        Deletes content starting at the specified position and ending at the specified position, and returns the string buffer object itself.

9. Code demonstration of delete function:

public class StringBufferDemo {
	public static void main(String[] args) {
		// create object
		StringBuffer sb = new StringBuffer();

		// add function
		sb.append("hello").append("world").append("java");
		System.out.println("sb:" + sb);

		// public StringBuffer deleteCharAt(int index): delete the character at the specified position and return itself
		// Requirement: I want to delete the character e, what should I do?
		// sb.deleteCharAt(1);
		
		// Requirement: I want to delete the first l character, what should I do?
		// sb.deleteCharAt(1);

		// public StringBuffer delete(int start,int
		// end): delete the content starting from the specified position and ending at the specified position, and return itself
		// Requirement: I want to delete the string world, what should I do? (Include the left side, not the right side.)
		// sb.delete (5, 10);

		// The empty function of the string buffer: I want to delete all the data!
		sb.delete(0, sb.length());

		System.out.println("sb:" + sb);
	}
}

10. Replacement function of StringBuffer:

(1)public StringBuffer replace(int start,int end,String str)

            Replace with str from start to end, including the left position, but not the right position, and return the string buffer object itself.

11. Code demonstration of replacement function

public class StringBufferDemo {
	public static void main(String[] args) {
		// create string buffer object
		StringBuffer sb = new StringBuffer();

		// adding data
		sb.append("hello");
		sb.append("world");
		sb.append("java");
		System.out.println("sb:" + sb);

		// public StringBuffer replace(int start,int end,String
		// str):从start开始到end用str替换
		// 需求:我要把world这个数据替换为"节日快乐"
		sb.replace(5, 10, "节日快乐");
		System.out.println("sb:" + sb);
	}
}

12、StringBuffer的反转功能:

(1)public StringBuffer reverse( )

        将给定的字符串缓存区对象进行反转,并返回字符串缓存区对象本身。

13、反转功能的代码演示:

public class StringBufferDemo {
	public static void main(String[] args) {
		// 创建字符串缓冲区对象
		StringBuffer sb = new StringBuffer();

		// 添加数据
		sb.append("霞青林爱我");
		System.out.println("sb:" + sb);

		// public StringBuffer reverse()
		sb.reverse();
		System.out.println("sb:" + sb);
	}
}

14、StringBuffer的截取功能:

(1)public String substring(int start)

        从start位置开始到最后----截取字符串缓冲区中的内容,并返回一个String类型的对象。

(2)public String substring(int start, int end)

        从start位置到end位置结束(包左不包右)----截取字符串缓冲区中的内容,并返回一个String类型的对象。

        注意:返回值类型不再是StringBuffer本身了,而是String对象了。

15、截取功能的代码演示:

public class StringBufferDemo {
	public static void main(String[] args) {
		// 创建字符串缓冲区对象
		StringBuffer sb = new StringBuffer();

		// 添加元素
		sb.append("hello").append("world").append("java");
		System.out.println("sb:" + sb);	//helloworldjava

		// 截取功能
		// public String substring(int start)
		String s = sb.substring(5);	
		System.out.println("s:" + s);	//worldjava
		System.out.println("sb:" + sb);	//helloworldjava

		// public String substring(int start,int end)
		String ss = sb.substring(5, 10);
		System.out.println("ss:" + ss);	//world
		System.out.println("sb:" + sb);	//helloworldjava
	}
}



StringBuffer类——构造方法、字符串缓存区的容量和长度、添加、删除、替换、反转、截取功能



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324531925&siteId=291194637
Recommended