[Think in Java] Chapter 13th string

Source: Internet
Author: User

Immutable string

The string object is immutable. There is not a method in the string class that looks to modify a string value, in effect creating a completely new string object.


Overloading "+" with StringBuilder

Java overloads the ' + ' operator for a string object. such as: String test = "abc" + "def" + "GH"; As we mentioned above, the string is immutable, and Java will be a string object to link "abc" and "Def", then create a string to link just the resulting string and "GH" to generate the final string. This approach produces a whole bunch of intermediate objects that need to be garbage collected. But the Java compiler will help you optimize the code, the compiler will create a StringBuilder object, use the Append () method to add three pieces of content, and then call the ToString () method to produce the final string object. You can also use the JDK's own tool JAVAP to decompile the above code. The command is as follows:

Javap-c Test
-C indicates the generation of JVM bytecode. However, the level of compiler optimizations is limited.

public class Usingstringbuilder{pulblic static random rand = new random;p ublic String toString () {StringBuilder result = new StringBuilder ("["); for (int i = 0; I < 25; i++) {result.append (rand.nextint); Result.append (",");} Result.delete (Result.length ()-2, Result.length ()); Result.append ("]"); return result.tostring ();}}

As in the above example, if you merge the two append within the loop, append (Rand.nextint (100) + ","), then the compiler will fall into the trap, creating another StringBuilder object for you to handle the strings in parentheses. Frequent creation of StringBuilder can also cause significant overhead. It is recommended that you create a StringBuilder object yourself if you want to use string's join operation in a loop.

StringBuilder is introduced by Java SE5, which is thread-safe with stringbuffer.stringbuffer before, so the overhead will be great. So there is no threading problem when it is recommended to use StringBuilder.


Formatted output

The format method introduced by Java SE5 can be used with printstream or objects, and can of course be used with System.out objects. The format method mimics the printf () from C.

Fommatter class

In Java, all new formatting features are handled by the Java.util.Formatter class. The formatting syntax is as follows:

%[argument_index$][flags][width][.precision]conversion

The width is used to control the minimum size of the field. "-" flag is used to indicate the direction of the data, and the default is right-justified, plus "-" is left-justified. Precision is the number of decimal places for floating-point numbers, and an exception is triggered for integers.

Common Type Conversions

D           Integer (decimal)            e           Floating-point number C            Unicode character             x            integer (hexadecimal) b            boolean value                H            hash code (hex) s             String                       F              Floating-point number (decimal)
Note: For a Boolean or Boolean object, the conversion result is the corresponding true or FALSE for the B conversion used. but for other types, as long as it is not NULL, the result of the conversion is true, even if the number is 0! This is not the same as what we think, unlike the C language.


String.Format ()

String.Format () is a static method that accepts the same arguments as the Formatter.format method, but returns a string. In fact, inside the String.Format () method, a formatter object is created.


Regular expressions

String.match () checks whether a string matches a regular expression.

The string class has a tool that comes with a regular expression, the split () method, whose function is "to cut a string from a regular expression match." String.Split () also has an overloaded version that allows you to limit the number of string splits.

String.Replace is the last regular expression tool for the String class.

To understand the relevant syntax and meta-characters of regular expressions, you can refer to this article and write it very well. Regular expression 30-minute introductory tutorial. There may be some differences in the implementation of regular expressions for specific languages, but they are generally consistent.

[Think in Java] Chapter 13th string

Related Article

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.