Java Core API--2 (String, StringBuilder, StringBuffer)

Source: Internet
Author: User

1. String strings

1) Description of String type

The string class is final decorated, and the string object is an immutable object, and once created in memory, the content cannot be changed, and a new object is created to change the contents of the string.

A string object is created with static and dynamic points.

String str1 = "Dadsadad";

String str2 = new String ("Dasdasdadad");

2) string constant pool

The JVM has a restriction on strings, so that strings are used as immutable objects so that they can be reused. In fact, when we initialize a string by literal, constant, the JVM first starts with the Chang of the string (a memory area maintained within a JVM, which is used to hold a string object that has already been created), and the object that is used to hold the string is present, and if it exists, it is directly referenced. If it does not exist, create the string object and store it in a constant pool, and then reference it.

3) Common methods of strings

String str = "dn1aaj2fd6hbf4hjax0f";

①str.length (), gets the length of the string, returns the numeric value of type int

②str.indexof (char c), gets the index of the first occurrence of a character in a string, is not found, and the return value is-1.

Str.lastindexof (char c), which returns the index of the last occurrence of the specified character in this string , is not found, and the return value is-1.

③ str.charat (int index), gets the string that specifies the character at the subscript position, and the return value is of type char.

④ str.touppercase (), turns the string str all to uppercase,

Str.tolowercase (), turns the string str all to lowercase and returns a new string.

⑤str.stratswith (string s), which determines whether the string str starts with the string s,

Str.endswith (string s), determines whether the string str ends with S, and the return value is of type Boolean.

⑥ str.equals (string s), determines whether the contents of the two strings are equal (the difference between = = and Equals () has been parsed in the previous chapter) and returns a Boolean type.

⑦ str.replace (char OldChar, char Newchar): Converts all characters in a string that match Odchar to a new Newchar character, returning a new string.

⑧ str.substring (int begin), intercepts a paragraph of a character in a string, starts at the subscript begin and intercepts to the last,

str.substring (int begin, int end), intercepts a segment of a character in a string, starts at the subscript begin, ends at End-1, and returns a new string.

⑨ str.split (string regex): Parameter regex is a string rule that splits a string into an array of strings, with the string represented by the regex as a delimiter.

Where the regex representation of the string is not preserved, that is not stored in the string array, it can be understood to be fits, disappear!

⑩ Str.trim (), remove the spaces at both ends of the string and return the new string.

Case 1:

650) this.width=650; "style=" width:651px;height:515px; "title=" Clipboard.png "src=" http://s3.51cto.com/wyfs02/ M02/6d/af/wkiom1vpxfuqqmbraaswtinybjq543.jpg "width=" 659 "height=" 519 "alt=" wkiom1vpxfuqqmbraaswtinybjq543.jpg "/ >

Results:

650) this.width=650; "style=" WIDTH:503PX;HEIGHT:259PX; "title=" Clipboard.png "src=" http://s3.51cto.com/wyfs02/ M01/6d/ae/wkiom1vpxdli6rvtaac6ccqzdhe717.jpg "width=" 648 "height=" 481 "alt=" wkiom1vpxdli6rvtaac6ccqzdhe717.jpg "/ >


2. StringBuilder, StringBuffer class

We have learned from the string class that it is an immutable object, so the creation of new objects is thrown whenever the content is modified. So when we have a need to frequently modify the string, this does not only not reduce the memory overhead, the return will increase the memory overhead. To this end Java provides us with a class StringBuilder class that is designed to modify the contents of a string, which encapsulates a mutable string, in other words, when we need to change the contents of a string, we do not create a new object, but rather modify it on the basis of the original object. This reduces the overhead of memory.

Common methods of the StringBuilder class are:

Append (String str): Append string;

Insert (int index,string s): Inserts a string from the specified subscript position;

Delete (int start,int end): Removes the string from the starting subscript to end the subscript, and end is greater than or equal to start, which can exceed the total length of the array;

replace (int start,int end,string str): replace string;

Reverse (): string inversion.

Case 2:

650) this.width=650; "title=" Clipboard.png "src=" http://s3.51cto.com/wyfs02/M00/6D/AA/ Wkiol1vpxk6s6kt1aaidetsmaoa374.jpg "alt=" Wkiol1vpxk6s6kt1aaidetsmaoa374.jpg "/>

Results: It can be seen that the efficiency of the contrast is very strong.

650) this.width=650; "title=" Clipboard.png "src=" Http://s3.51cto.com/wyfs02/M01/6D/AA/wKioL1VpXmHy_ Mrraaagwgn4fow859.jpg "alt=" Wkiol1vpxmhy_mrraaagwgn4fow859.jpg "/>

Case 3:

650) this.width=650; "style=" width:662px;height:298px; "title=" Clipboard.png "src=" http://s3.51cto.com/wyfs02/ M00/6d/aa/wkiol1vpxoghgipkaag5xr3z9i4780.jpg "width=" 677 "height=" 266 "alt=" wkiol1vpxoghgipkaag5xr3z9i4780.jpg "/ >

Results:

650) this.width=650; "title=" Clipboard.png "src=" http://s3.51cto.com/wyfs02/M00/6D/AF/ Wkiom1vpxq3ybtgkaabdwasbl9u071.jpg "alt=" Wkiom1vpxq3ybtgkaabdwasbl9u071.jpg "/>

The StringBuffer class, which is an early version of the JDK, StringBuffer is a thread-safe variable character sequence. Although it contains a specific sequence of characters at any point in time, some method calls can change the length and content of the sequence. String buffers can be safely used with multiple threads. These methods can be synchronized if necessary, so all operations on any particular instance appear to occur in a serial order that is consistent with the sequence of method calls made by each thread involved. The main operations on StringBuffer are the Append and insert methods, which can be overloaded to accept arbitrary types of data. Each method effectively converts the given data into a string, and then appends or inserts the character of the string into the string buffer.

Typically, if SB refers to an instance of StringBuilder, Sb.append (x) and Sb.insert (Sb.length (), x) have the same effect. Whenever an operation occurs about a source sequence, such as appending or inserting in a source sequence, the class only implements synchronization on the string buffer that performs this operation instead of on the source. Each string buffer has a certain amount of capacity. As long as the string buffer contains a sequence of characters that does not exceed this capacity, there is no need to allocate a new array of internal buffers. If an internal buffer overflows, this capacity is automatically incremented.

Starting with JDK 5, this class complements the equivalence class used by a single thread, StringBuilder. The StringBuilder class should usually be preferred over this class because it supports all the same operations, but it is faster because it does not perform synchronization. Java.lang.StringBuilder A variable sequence of characters. This class provides an API that is compatible with StringBuffer, but does not guarantee synchronization. This class is designed to be used as a simple replacement for stringbuffer, which is common when a string buffer is used by a single thread. If possible, it is recommended that this class be preferred because, in most implementations, it is faster than StringBuffer.


This article is from "Forever Young" blog, please be sure to keep this source http://kingkongzhao.blog.51cto.com/6319491/1656611

Java Core API--2 (String, StringBuilder, StringBuffer)

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.