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.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. 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"));
}
}
</SPAN>
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));
n++;
if (n%16==0)
Result.append (' \ n ');
}
return result.tostring ();
}
}
</SPAN>