Deep analysis of Java strings and formatted output _java

Source: Internet
Author: User
Tags string format string indexof stringbuffer

1, String class, StringBuilder class, StringBuffer class
The string object is immutable, overloaded with operator +, and the string s= "a" +2+ "B" +2.2; This statement creates 4 string object objects, assigns the last object reference to S.

However, the string class defines many common methods for manipulating strings: Take the length of the string, determine whether it is an empty string isempty, return a character array or byte array ToCharArray (), obtain the character charat () of the specified index, The string compares equals () CompareTo (), converts the character to uppercase or lowercase tolowercase (), begins with or ends Startwith (), determines whether a character contains (), the index string indexof (), Gets substring substring (), string concatenation concat (), string instead of replace (), remove character ends space trim (), return string object representing parameter content valueof (), split string return string array split (). Note that the regular expression is passed in when splitting. The string class does not provide the ability to flip strings.

String objects are immutable, so you often use the StringBuilder class to construct strings. The StringBuilder class provides features such as string concatenation, deletion of individual characters, deletion of specified character sequences, insertion of characters, and so on. If you want to ensure thread safety, you should use the StringBuffer class, the method is the same as StringBuilder.

2, formatted output
The following example formats the output in Java in the console and file

Copy Code code as follows:

<span style= "font-size:16px" >package demo.others;

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. Two simplest formatted output, similar to the printf function in C language
System.out.format ("%-3d%-5.3f\n", I, D);
System.out.printf ("%-3d%-5.3f\n", I, D);
Use of the Formatter class
2. Format output to console
Formatter f = new Formatter (System.out);
F.format ("%-3d%-8.2f%-10s\n", I, D, "touch");
3. Format output to 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 () with C language ()
System.out.println (String.Format ("(%d%.2f%s)", I, D, "touch"));

3, the tool class to view binary files in hexadecimal
Copy Code code as follows:

<span style= "font-size:16px" >package mine.util.others;

* View binary files in hexadecimal
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));
if (n%16==0)
Result.append (' \ n ');
return result.tostring ();

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: 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.