Home  >  Article  >  Java  >  How to use StringBuilder function in Java

How to use StringBuilder function in Java

WBOY
WBOYOriginal
2023-06-26 21:12:081454browse

StringBuilder in Java is a class that allows you to modify strings without having to create new strings. Different from the String class, StringBuilder can add, delete, modify and query strings through append(), insert(), delete() and other methods, thereby changing the content of the original string. In many scenarios, using StringBuilder can significantly reduce memory overhead and program running time.

This article will introduce how to use StringBuilder to operate strings.

  1. Create a StringBuilder object

To use StringBuilder, you first need to create a StringBuilder object. This can be achieved through the constructor StringBuilder() or StringBuilder(String str).

StringBuilder sb1 = new StringBuilder(); // Create an empty StringBuilder object
StringBuilder sb2 = new StringBuilder("Hello World"); // When creating a new StringBuilder object, specify its initial value For "Hello World"

  1. Use StringBuilder object

2.1 Add a string to a StringBuilder object

To add a string to a StringBuilder object, You can use the append() method:

sb1.append("Hello"); // Add the "Hello" string to the sb1 object
sb1.append(" "); // Add a space
sb1.append("World"); //Add the "World" string

2.2 Delete the string from the StringBuilder object

By using the delete() method, you can delete the string from the StringBuilder object Delete a string from the object. When representing a range (the start and end of the range), the indexes provided are 0-based indexes.

sb1.delete(0, 5); // Delete 5 characters from the starting point
sb1.deleteCharAt(3); // Delete the character with index 3

2.3 in Inserting a string into the StringBuilder object

You can use the insert() method to insert a new string at any position.

sb1.insert(0, "Hi "); // Insert the string "Hi " at the beginning

2.4 Find substrings in the StringBuilder object

You can use indexOf () method finds the position of a specific string in the StringBuilder object.

int index = sb1.indexOf("World"); // Find the position of "World" in sb1

2.5 Convert StringBuilder object to String

StringBuilder object The final goal is usually to convert it to the String type, which can be achieved using the toString() method:

String str = sb1.toString(); // Convert the sb1 object to the String type

  1. Advantages of StringBuilder

3.1 Mutability

The StringBuilder class allows modification of strings. If you use the immutable String class, you need to create a new string every time you modify the string, which will cause memory overhead and waste of time. Using StringBuilder avoids this overhead since it is a mutable string.

3.2 More efficient string concatenation

If you want to concatenate multiple strings, a common pattern when using the String class is to use the operator to concatenate them. However, this approach requires the creation of multiple String objects, because as a new string is added, the next anonymous String object is also created. For long strings, it can cause performance issues.

When StringBuilder splices strings, it only creates a string constructor and an internal character array, so it is much more efficient. It can be understood that StringBuilder is equivalent to a container of an internal array. Every time a new string is added, characters are added to the array one by one.

  1. Summary

StringBuilder is a useful class in Java that allows modification of strings without having to create new strings. The methods it provides make operating strings more convenient and efficient, and can save memory. StringBuilder is a good choice when you need to continuously add and modify strings.

The above is the detailed content of How to use StringBuilder function in Java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn