JAVA - String tools StringBuilder and StringBuffer

Article directory

Table of contents

Article directory

foreword

2. Demonstration of common methods

1.append() is used to add the specified string to the end of the current StringBuilder object

2.delete(): Used to delete the character at the specified position in the StringBuilder object.

 3.insert(): Used to insert the specified string at the specified position.

 4.replace(): Used to replace the character or string at the specified position.

5.reverse(): Used to reverse the order of characters in the StringBuilder object. 

 3. Construction method

 1.StringBuilder() creates an empty StringBuilder object.

2.StringBuilder(int capacity) Create a StringBuilder object with the specified initial capacity. 

 3.StringBuilder(String str) Create a StringBuilder object containing the specified string content.

4. The difference between StringBuilder and StringBuffer

Summarize



foreword

Hello everyone, I love animal milk the most. Today I will introduce you to the tool classes StringBuilder and StringBuffer for strings.


1. What is StringBuilder?

StringBuilder is a tool class for processing String

common method

  1. append(): used to add the specified string to the end of the current StringBuilder object.

  2. delete(): Used to delete the character at the specified position in the StringBuilder object.

  3. insert(): Used to insert the specified string at the specified position.

  4. replace(): Used to replace the character or string at the specified position.

  5. reverse(): Used to reverse the order of characters in the StringBuilder object.

2. Demonstration of common methods

1.append() is used to add the specified string to the end of the current StringBuilder object


2.delete(): Used to delete the character at the specified position in the StringBuilder object.

 3.insert(): Used to insert the specified string at the specified position.

 4.replace(): Used to replace the character or string at the specified position.

5.reverse(): Used to reverse the order of characters in the StringBuilder object. 

 3. Construction method

 

1.StringBuilder() creates an empty StringBuilder object.

2.StringBuilder(int capacity) Create a StringBuilder object with the specified initial capacity.

3.StringBuilder(String str) Create a StringBuilder object containing the specified string content.

 1.StringBuilder() creates an empty StringBuilder object.

We didn't specify a size, so what is the default space?

2.StringBuilder(int capacity) Create a StringBuilder object with the specified initial capacity. 

 

 3.StringBuilder(String str) Create a StringBuilder object containing the specified string content.

Don’t think it’s complicated!! The StringBuilder object can be directly regarded as an operable string. If you want to modify the string, then you can directly create the StringBuilder object, because the string cannot be modified

4. The difference between StringBuilder and StringBuffer

Both StringBuilder and StringBuffer are classes for processing strings, and the methods are basically the same. Their main difference is thread safety and performance.

StringBuffer: It is thread-safe because its methods are synchronized, but this also leads to its poor performance.

StringBuilder is not thread-safe , but its performance is better than StringBuffer because its methods are all asynchronous.

Therefore, in a single-threaded environment, it is recommended to use StringBuilder; in a multi-threaded environment, it is recommended to use StringBuffer

Before learning multi-threading, the methods used are basically single-threaded, and StringBuilder can be used directly

 The knowledge about multithreading will be mentioned later, so stay tuned~~

Summarize

The above is what I want to talk about today. This article briefly introduces the use of StringBuilder. You should focus on mastering the use of the method

Guess you like

Origin blog.csdn.net/weixin_73869209/article/details/130752708