Common Java parsing Class 8: Java string and formatted output

Source: Internet
Author: User
1. String, stringbuilder, and stringbuffer

The string object is immutable, and the operator + is overloaded, so string S = "A" + 2 + "B" + 2.2; this statement creates four String object objects, assign the last object reference to S.

However, the string class defines many common operations on strings: Taking the length of a string, determining whether it is an empty string isempty, returning a character array or byte array tochararray () the character charat () for obtaining the specified index, the character string comparison equals () compareto (), the character conversion into uppercase or lowercase tolowercase (), and the start or end of startwith () determine whether or not to include a specific character contains (), index string indexof (), obtain substring (), string connection Concat (), string replace Replace () remove space trim () at both ends of the character, return the string object valueof () that represents the parameter content, and return the string array split (). Note that the regular expression is input during split. The string class does not provide the string flip function.

The string object is immutable, so the stringbuilder class is often used to construct the string. The stringbuilder class provides functions such as string connection, deleting a single character, deleting a specified character sequence, and inserting characters. To ensure thread security, use the stringbuffer class. The method is the same as that of stringbuilder.

2. format the output

The following example shows how to format and output data to the console and files in Java.

Package demo. others; import Java. io. filenotfoundexception; import Java. io. printstream; import Java. util. formatter;/*** formatter class for formatting ** @ author touch **/public class formatterdemo {public static void main (string [] ARGs) {int I = 1; double D = 2.2352353456345; // 1. the two simplest formats of output are similar to the printf function system in C language. out. format ("%-3D %-5.3f \ n", I, d); system. out. printf ("%-3D %-5.3f \ n", I, d); // use of the formatter class // 2. format the output to the console formatter F = new formatter (system. out); F. format ("%-3D %-8.2f %-10s \ n", I, D, "Touch"); // 3. format the output to the file formatter FF = NULL; try {FF = new formatter (New printstream ("file/formater.txt");} catch (filenotfoundexception e) {e. printstacktrace ();} ff. format ("%-3D %-8.2f %-10s \ n", I, D, "Touch"); // 4. string. format (). sprintf () system in C language. out. println (string. format ("(% d %. 2f % s) ", I, D," Touch "));}}
3. Tool class for viewing binary files in hexadecimal format
Package mine. util. others;/*** view the binary file in hexadecimal format */public class hex {public static string format (byte [] data) {stringbuilder result = new stringbuilder (); int n = 0; For (byte B: Data) {If (N % 16 = 0) result. append (string. format ("% 05x:", n); result. append (string. format ("% 02x", B); N ++; If (N % 16 = 0) result. append ('\ n');} return result. tostring ();}}

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.