6 String
6.1, the characteristics of string
String s= "ABC"; s is a class-type variable, "ABC" is an object
Once the string is initialized, it cannot be changed.
String S1=new string ("abc");
What is the difference between S and S1:
S has an object in memory, S1 has two objects in memory
6.2 Common methods
1. Get
int length ();
char charAt (int index); Gets the character at that position, depending on the position
int indexOf (int ch); Gets the position of the first occurrence of the character in the string according to the CH character
int indexOf (int ch,int formindex); get the ch character where the string appears, starting at the Formindex specified position
int IndexOf (string s); Gets the position where the first occurrence of the string is based on s
int indexOf (String s,int formindex), starting at the Formindex specified position, get s where the string appears
Stringindexoutofboundsexception occurs when accessing a corner label that does not exist in a string
If not found, return-1
int Lasetindexof (); The position is reversed from the last position of the string, and the usage is the same as indexof.
2. Judgment
Boolean startwith (String str), whether the string starts with Str
Boolean endsWith (String str), whether the string ends with Str
Boolean isEmpty (); string is empty
Boolean contains (string str), whether the string contains str
Boolean equals (String str) to determine whether the string contents are the same
Boolean equalsignorecase (String str), ignoring case to determine if string is the same
3. Conversion
The character array is converted to a string:
Constructor: String (char[])
String (Char[],offset,count) converts a portion of a character array into a string
Static methods: Static String copyvalueof (char[] data);
Static String copyvalueof (char[] data,int offset,int count);
The byte array is converted to a string:
Constructor: String (byte[])
String (Byte[],offset,count) converts a portion of a character array into a string
The basic data class turns into a string:
static String valueOf (int);
Static String valueOf (double);
The string is converted to a character array/byte array:
Char[] ToCharArray ();
Byte[] GetBytes ();
4. Replace
String replace (Oldchar,newchar);
If the character to be replaced does not exist, the original string is returned
String replace (OLDSTR,NEWSTR);
5. Cutting
String[] Split (regex);
6, sub-string
String substring (begin);
String substring (begin,end), containing the header, not including the tail
Get part of a string
7. Conversion
String toUpperCase (); String toLowerCase (); strings are converted to uppercase or lowercase
string trim (); Remove spaces at both ends of the string
int CompareTo (string), a natural order comparison of two strings
6.3 StringBuffer
StringBuffer is a container that is variable in length and can be modified for the contents of a string
1. Insert and delete
StringBuffer append (); Add the data as a parameter to the end of the existing data
StringBuffer Insert (index,data); You can add data to the specified location
StringBuffer Delete (int start,int end); Delete data containing start does not contain end
StringBuffer deletecharat (int index); Delete the character at the specified position
2. Get the location of the string:
int indexOf (String s);
int lastIndexOf (String s);
int length ();
3. Positioning
char charAt (int index); Gets the character at the specified position:
StringBuffer replace (int start,int end,string s), from start position to end position, replaced by s
void Setcharat (int index,char ch); Sets the character at the specified position
4. After the JDK version 1.5, StringBuilder appeared.
StringBuffer is thread synchronization
StringBuilder is thread not synchronizing
6.4 Basic data type Object wrapper class
Basic data wrapper class
BYTE byte
Short Short
Long Long
int Integer
Boolean Boolean
float float
Double Double
Char Character
1, the most common role:
Conversions between basic data types and strings
2. The basic data type is converted into a string
Basic data type + "";
Wrapper class. toString (base data type value);
such as: Integer.tostring (36);//Change 36 to "36"
3. String converted to basic data type
Static wrapper class. Parse basic data type (String s);
Object. Basic data Type value ();
6.5 Decimal and other binary reciprocal transfers
1, decimal into other binary
Tobinarystring (); Turn binary
Tohexstring (); turn octal
Tooctalstring (); Turn hex
2. Convert other binary into decimal
parseint (String,radix);
6.6 New Features
Integer x = 4; auto-boxing, 4 equals new Integer (4)
x = x +2; automatically unpacking, after the operation of the x into the int type, the box is assigned to X
Integer m = 128;
Integer n = 128;
m==n;//result bit false
Integer a = 127;
Integer B = 127;
The a==b;//result is true because A and B point to the same integer object. When the value is within the byte range, if the value already exists, no new space will be opened.
This article is from the "Java Basics" blog, so be sure to keep this source http://8163413.blog.51cto.com/8153413/1712323
Sixth Chapter String