Common classes
String Input=joptionpane.showinputdialog ();//Input box
Joptionpane.showmessagedialog (,)//message box
String class
string str0 = "Hello";//String constant "Hello" is pre-placed in the string constant pool of the data segment
String str1 = "Hello";//search for existing string constants directly from the constant pool
String str2 = new String ("Hello");//new a new string object in the heap
String str3 = new String ("Hello");//new a new string object in the heap
A String type object (whether it is a constant pool object or an object in the heap, its contents are immutable)
STR3 = str2;
STR2 = "World";
String causes some operators to change
System.out.println ("5" + 10); As long as a string appears before the//+, it becomes a string connector
string comparison
System.out.println (Str0.equals (STR1));//Determine whether two strings are equal
System.out.println (Str0.equalsignorecase ("HeLLo"));//Ignore case comparison equal
System.out.println ("Hello". CompareTo ("World"));//string comparison: First compares the ASCII code difference of the first different letter in turn, if all the same is longer than the string length
System.out.println ("Hello". Comparetoignorecase ("HelloWorld"));
A method in a string that is related to a character array
System.out.println (Str0.length ());//String length
System.out.println (Str0.charat (0));//What character is the first position of the string
char[] Strarray = Str0.tochararray ();//Convert a string to a character array
Byte[] B = str0.getbytes ();//Convert a string to a byte array
System.out.println (Str0.indexof (' l '));//The subscript for the first occurrence of a character in a string
System.out.println (Str0.lastindexof (' l '));//The last occurrence of a character in a string
String content-related methods
System.out.println (Str0.touppercase ());//Convert to full capitalization
System.out.println (Str0.tolowercase ());//convert to full lowercase
System.out.println (Str0.startswith ("wor"));//Determine what the string begins with
System.out.println (Str0.endswith ("Lo"));//Determine what the string ends with
System.out.println (Str0.contains ("ell"));//Determine if a string is in another string--contains
System.out.println (Str0.concat ("World"));//Add a string to the end of another string
System.out.println (Str0.replace (' l ', ' l '));//Replace a character in a string with a new character
System.out.println (Str0.replace ("L", "fuck"));//Replace a substring in a string with a new substring
System.out.println (str0.substring (1,3));//The string is intercepted by position, and the opening interval of the front closed
System.out.println (str0.substring (2));
String Brute Force method
Remove the front and back space of the string, mainly used when receiving input
String STR4 = "Hello World";
String STR5 = Str4.trim ();
System.out.println (STR5);
String splitting
String birthday = "1990-5-8";
String[] results = Birthday.split ("-");
for (String tmp:results) {
SYSTEM.OUT.PRINTLN (TMP);
}
birthday= "5-8-";
Results = Birthday.split ("-");
System.out.println (results.length);
Regular expressions
"" represents a character.
{M,n} indicates a minimum of m times and a maximum of n occurrences.
(|) group
Strings that appear in a constant pool are of type string.
StringBuffer cannot complete string concatenation with the "+" sign.
stringbuffer--content is mutable, so it does not create new objects when strings are connected, but increases them on existing objects.
Use it when you need to do a lot of string linking operations.
StringBuffer sb = new StringBuffer ("Hello");
SB = Sb.append ("World");//Add data to the end of the string represented by StringBuffer.
Sb.insert (5, ");
System.out.println (SB);
String str = "HelloWorld";
String beginstr = str.substring (0,5);
String endstr = str.substring (5);
str = beginstr + "" + endstr;
Sb.insert (); Inserts a new content into the string.
Thread-safe, low-efficiency. Threads are unsafe and highly efficient.
Performance: Stringbuilder>stringbuffer>string.
1, the current time date, commonly used date
2, when the date of comparison, commonly used
3, when printing the date string, commonly used---simpledateformat class with
4, according to the set to get the specified date, using date inconvenient, recommended to use the Calendar class
Calendar cal = Calendar.getinstance ();
Two ways to convert a calendar to date
Date date = Cal.gettime ();
Date = new Date (Cal.gettimeinmillis ());
Packing class
The value in the wrapper class is immutable.
//Basic turn wrapper
int i0 = ten;
Integer in0 = new Integer (i0);//First: Call the wrapper class with the constructor of the basic type argument to construct
Integer in00 = i0;//The second way: After JDK1.5, the automatic enclosure
Object obj = i0;//is also the first to automatically marshal the box, and then let the parent class reference point to the Subclass object
System.out.println (in0);
//wrapper to basic
integer in = new integer;
int i = In.intvalue ();//The first way: Call wrapper class object ***value ()
int i0 = in;//The second way: JDK1.5, auto-unpacking
System.out.println (i);
//wrapper to String
integer in = new Integer (123);
String str = in.tostring ();//Call the wrapper class object ToString ()
System.out.println (str);
//string to wrap
String str = "321";
Integer in = new Integer (str);//constructor with string argument for wrapper class
SYSTEM.OUT.PRINTLN (in);
//Basic turn string
int i = 9527;
String str = integer.tostring (i);//The first way: Use the wrapper class with the parameter ToString ()
String str0 = i + "";//The second shameless way
System.out.println (str);
//The most important--string to basic
String str = "54188";
int i = Integer.parseint (str);//Call wrapper class parse*** ()
System.out.println (i);
Chapter 12 Summary