Use StringBuilder to process strings more efficiently

Source: Internet
Author: User

Preface

This article introduces a java string processing technique-using StringBuilder to improve the string processing efficiency.

StringBuilder

When the program needs to splice strings frequently, the efficiency of using strings directly is relatively low.

The internal implementation of StringBuilder optimizes the processing of concatenation operations. Therefore, when you need to splice a String frequently, this class should be used first instead of the String class.

Procedure

1. Define the StringBuilder object

2. Concatenate strings

3. Convert the StringBuilder object back to the String object

Sample code

1 package test; 2 3 // demonstrate the standard String concatenation method 4 public class Test {5 6 public static void main (String [] args) {7 8 // Step 1: construct a StringBuilder object 9 StringBuilder builder = new StringBuilder (); 10 11 // Step 2: splice the String 12 String str = "test String" to the object; 13 builder. append (str); 14 15 // Step 3: convert the StringBuilder object to the String object 16 str = builder. toString (); 17 18 // Display result 19 System. out. print (str); 20} 21}

Running result

Summary

1. This class is specially used to process string concatenation.

1. If thread security support is required, use StringBuffer.

Use StringBuilder to process strings more efficiently

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.