String and StringBuilder (2), string String operations under the string connection operation

Source: Internet
Author: User

String and StringBuilder (2), string String operations under the string connection operation

Focus on the append method of StringBuilder

 

Use source insight to view the source code provided in Jdk and locate StringBuilder. java.

An overload of the append method located in the search append Method

    public StringBuilder append(String str) {    super.append(str);        return this;    }

It can be seen that the append method of the parent class is called.

 

Locate the append (String str) method of the parent class

    public AbstractStringBuilder append(String str) {    if (str == null) str = "null";        int len = str.length();    if (len == 0) return this;    int newCount = count + len;    if (newCount > value.length)        expandCapacity(newCount);    str.getChars(0, len, value, count);    count = newCount;    return this;    }

The case where str is null and Len is 0 is not described.

 

Value definition (char array)

   /**     * The value is used for character storage.     */    char value[];

The definition of count (used in the array)

    /**      * The count is the number of characters used.     */    int count;

 

int newCount = count + len;    if (newCount > value.length)        expandCapacity(newCount);

 

NewCount obtains the new length and the default character array length for comparison.

If the length of a string after append exceeds the length of the default value array, resize the string.

    /**     * This implements the expansion semantics of ensureCapacity with no     * size check or synchronization.     */    void expandCapacity(int minimumCapacity) {    int newCapacity = (value.length + 1) * 2;        if (newCapacity < 0) {            newCapacity = Integer.MAX_VALUE;        } else if (minimumCapacity > newCapacity) {        newCapacity = minimumCapacity;    }        value = Arrays.copyOf(value, newCapacity);    }

Char array capacity expanded to 2 times

Copy the original data to the new array.

 

The Return Value Type of the append method of the subclass is StringBuilder. The parent class is AbstractStringBuilder.

The conversion between them involves an upward or downward transformation.

 

Other append overload method operations are similar.


What are the different connection methods for strings in ASPNET?

The Stringbuilder class is directly used for string operations. For example
(1) string aa = "123456 ";
(2) aa ++ = "789 ";

(3) StringBuilder text = new StringBuilder ("123456", 12 );
(4) text. Append ("789 ");
If you output aa, and text, you will find that their output content is the same.
But the aa operation is actually: first, allocate an address space in the memory, and the size of the space is 6.
Then, perform the aa + = "789"; operation, which is the connection string, "123456" and "789" and re-allocate the address in the memory. Point the memory address of aa to the memory address of "123456789.

That is to say, in the memory, there are actually two spaces allocated to the north. The first memory space was automatically processed by the C # garbage disposal system later,

If we use a three or four-sentence program to implement this process, it does not allocate memory space again,
It operates in the memory space of text. Here we need to explain that StringBuilder can allocate its own size during the life variable process. If the actual content exceeds the memory space,
It will automatically double.

Through the above example, we can know that the superiority of StringBuilder is:
First, he does not need to allocate memory every time. Therefore, the system does not have to deal with garbage;
Second, when we need to perform multiple operations on a string multiple times, its efficiency is far higher than that of string.

Hope the answer is helpful to you;

What is the difference between the String class and the StringBuilder class? Why do these two classes exist simultaneously in the Net class library? Please

If you want to operate on a growing String, use the StringBuilder class instead of the String class. The two classes work in different ways: the String class is a traditional way of modifying strings. It can indeed add a String to another String, but in.. Because the system first writes two strings to the memory, then deletes the original String object, creates a String object, and reads the data in the memory and assigns it. This process takes a lot of time. The StringBuilder class under the System. Text namespace is not like this. It provides the Append method, which can be used to modify strings in the existing object, which is simple and direct. Of course, the efficiency difference between the two is usually not noticed, but if you want to add a large number of operations to a string, the time consumed by the StringBuilder class and the String class are not an order of magnitude.

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.