Learning content summary
String
class- Java provides a special sequence of characters to deal with
String
class String
Classjava.lang
package, since thejava.lang
package default class is introduced, so the program can directly useString
the class- Note that the Java
String
class declaration for thefinal
class, so the user can not extendString
classes, namelyString
class can not have subclasses
- Java provides a special sequence of characters to deal with
- Construction
String
objects- Const object
- String constants are enclosed in double quotes target sequence of characters
- Constant pool: constant pool of data no longer allowed to change during program execution
String
Objects- You can use
String
the class declaration objects and create objects - Users can not output
String
object reference - You can use an already created
String
object creates anotherString
target - Two more common method of construction
String (char a[])
Create an array of a character with aString
targetString(char a[],int startIndex,int count)
Extracting a character array of a part of a character to createString
objects, the parametersstartIndex
andcount
specify a starting position of a character extraction and the number of characters from a starting position, taken in the
- Reference
String
constantsString
Constants are objects, so you can putString
constant references assigned to aString
target
- You can use
- Const object
- String juxtaposition
String
Object can+
be the concatenation operation, i.e., end to end, to obtain a newString
target- If two constants concatenation operation, then still get constant, constant pool if you do not have this constant into the constant pool
String
Common methods of the classpublic int length()
: Obtaining a character string lengthpublic boolean equals(String s)
: Judging whether the currentString
object is a character sequence is specified by the parameter sString
character sequence identical objectpublic boolean startsWith(String s)
: Judging whether the currentString
object is a character sequence parameter specifies whether the prefixString
character sequence s of objectspublic int compareTo(String s)
: Lexicographical ordering parameter s specifies the size of a character sequence comparisonpublic boolean contains(String s)
:String
Object callscontains
the method determines whether the currentString
object contains a character sequence character sequence parameter spublic int indexOf (String str)
:String
Object invoke a method from the currentString
start retrieving the index position 0 character sequence of the target position of the character sequence of str the first time, and returns the positionpublic String substring(int startpoint)
: String object that calls the method to obtain a newString
target, the newString
character sequence to copy the current object is theString
object of the character sequencestartpoint
character sequence position to the last position of the character obtained.String
Calling the objectsubstring(int start ,int end)
method for obtaining a newString
target, the newString
character sequence to copy the current object is theString
object of the character sequencestart
position to theend–1
character sequence of characters at the position of the obtainedpublic String trim()
: Get a newString
object, and this newString
character sequence is the object of the currentString
sequence of characters before and after the character sequence objects removed after spaces
- Interconversion basic string data
- Java.lang package Integer class calls the class method which:
public static int parseInt(String s)
can by the string "number" characters, converted to an int - Use java.lang package Byte, Short, Long, Float, Double tone corresponding class by class methods string "number" characters, converted to the corresponding basic data types
- Can
String
class class methodpublic static String valueOf(n的类型, n)
, the corresponding value into a string
- Java.lang package Integer class calls the class method which:
- A string representation of the object
- An object by calling the
public String toString()
get method of the object character sequence. An object calltoString()
method returnsString
general form of a sequence of characters object is:
a quoted string to create the object names @ object class representation
- An object by calling the
- Character strings, byte array
- String array of characters
- String class constructor:
String(char[]);
;String(char[],int offset,int length)
each string object created using all characters and character array portion of the character - The partial sequence of characters stored in the String object array method:
public void getChars(int start,int end,char c[],int offset )
- The character sequence stored in the method of the String object array:
public char[] toCharArray()
- String class constructor:
- Strings and bytes
String(byte[])
It constructs a string object using the specified byte array.String(byte[],int offset,int length)
With a portion of the specified byte array, i.e., length bytes starting at offset to take objects from an array configuration of a string starting positionpublic byte[] getBytes()
Using the platform's default character encoding, storing the sequence of characters currently String object into a byte array, and returns a reference to the array.public byte[] getBytes(String charsetName)
Parameter specifies the character encoding, the sequence of characters currently stored in the String object into a byte array, and returns a reference to an array
- Encryption algorithm string
char [] p = password.toCharArray();
- String array of characters
- StringTokenizer类
- Two common constructors:
StringTokenizer(String s)
: A parser configured as a String object s. Default division marks, i.e. the space character, line feed, carriage return, Tab character, feed character as separator markStringTokenizer(String s, String delim)
: A parser configured as a String object s. Any sequence of characters arranged in character delim parameter is labeled as a separator
StringTokenizer
A string parser objects referred to can use the following methods:nextToken()
: Get-by-language symbols (words) in the string, the string value analyzer responsible count variable is decreased by ahasMoreTokens()
: As long as there are language character string symbol, i.e. the value of the count variable is greater than 0, then the method returns true, false otherwisecountTokens()
: Get the value of the count variable analyzer
- Two common constructors:
Scanner
class- Scanner class using the data analysis program from the required string
StringBuffer
classStringBuffer
Memory entity object class can automatically change the size, easy to store a sequence of characters variable- Common method
StringBuffer append(String s)
: A String object s character sequence is appended to the current sequence of characters StringBuffer object, and returns the current object's reference StringBufferStringBuffer append(int n)
: The conversion of n int type String object, and then the String object character sequence to the sequence of characters currently StringBuffer object, and returns the current object's reference StringBufferStringBuffer append(Object o)
: Object The object o a character sequence appended to the current sequence of characters StringBuffer object, and returns the current object's reference StringBufferpublic chat charAt(int n )
: To give n characters on a single character setpublic void setCharAt(int n ,char ch)
: The current character position n StringBuffer object entity specified string character replacement parameter chStringBuffer insert(int index, String str)
: Parameter str specified string into the location specified by the index parameterpublic StringBuffer reverse()
: The object entity characters flipStringBuffer delete(int startIndex, int endIndex)
: Deletes a substring from the current StringBuffer object entity in the stringStringBuffer replace(int startIndex,int endIndex,String str)
: A current substrings of a string StringBuffer object entity specified by parameter string replacement str
Date
AndCalendar
class- Date class
- Date class java.util package
- One class constructor Date:
Date nowTime=new Date();
- Class Calendar
- Calendar class java.util package
- Using static method getInstance Calendar class () to initialize a calendar object:
Calendar calendar= Calendar.getInstance();
- Date class
Class
Classes andConsole
class- Class class
- Class obtained using a class and a method associated with certain Class object (className parameter specified class),
public static Class forName(String className) throws ClassNotFoundException
the method returns a Class object class and associated parameter className specified. If the class is in a package, className must have the package name - Class object obtained in the above step is called
public Object newInstance() throws Exception
,
the method can obtain an object class className
special note: when using Class object call newInstance () to instantiate an object of class className, class className argument constructor must Have
- Class obtained using a class and a method associated with certain Class object (className parameter specified class),
Console
classConsole cons = System.console();
- Class class
Pattern
AndMatcher
class- Java classes and provides Pattern Matcher class devoted to the pattern matching, the classes in the package java.util.regex
- Build mode objects
- Using regular expressions as parameters to give a regex called Pattern mode instance of the class pattern
- Schema objects are regular expressions package. Pattern class class method calls the compile (String regex) Returns a schema object, wherein the parameter is a regular expression regex, referred to as mode pattern objects used
- Get matching objects
- Call mode object pattern matcher (CharSequence input) method returns a Matcher object matcher, referred to as a matching object
Other (perception, thinking, etc.)
no
Reference material
- "Java Programming"