First, create and initialize a string
String b = "Hello";
Create and initialize a string using the constructor method
string ();//Initialize strings, representing empty character sequences
string (value);//Create a new object with a string constant that already exists
String (char[] value);//Use a character array to create a string
String (char[] Value,int offset,int count);//intercept character array offset to count character create a non-empty string
String (stringbuffer buffer);//Initialize string object with StringBuffer object
Ii. use of the main methods of the string type:
1, get the length *.length ();//This is different from the acquisition length in the array, *.length;
2. Compare string (1) equals ()//Determine whether the content is the same
(2) CompareTo ()//judge the size relationship of the string
(3) comparetoignorecase (String int)//Ignore letter case when comparing
(4) = =//Determine whether the content and address are the same
(5) Equalsignorecase ()//Ignore case to determine whether the content is the same
If you want to compare parts of a string with the same content, you can use the
(6) Reagionmatches ()//There are two types of public boolean regionmatches (int toffset, String other,int ooffset,int len); Is true if one of the substring of a string object is the same sequence of characters as one of the substrings of parameter other. String objects to be compared start with the index Toffset, and other strings start with the index ooffset, len length.
Public Boolean reagionmatches (Boolean ignorecase,int toffset,string other,int ooffset,int len); Arguments with Boolean types indicate whether the comparison of two strings is case sensitive.
Find a character in a position in a string
public char charAt (int index)//returns the character at the index position of the specified index with the index range starting at 0
Find the position of the first or last word in the string that the specified string appears in
In the string class, you provide two methods for locating the first occurrence of a string in a specified position
(1) public int indexOf (string str);//start retrieving str from the string and returning to the first occurrence, not returning-1
(2) public int indexOf (string str,int fromindex);//start retrieving Str from the first fromindex character of the string
There are two ways to find the last occurrence of a position
(1) public int lastindexof (String str);
(2) public int lastindexof (String str,int fromindex);
You can use the public boolean contains (Charsequence s) If you do not care about the exact position of the string;
Check the start and end characters of the string
Two ways to start a string
(1) Public boolean starwith (string Prefix,int toffset);//If the string sequence represented by the parameter prefix is a substring of the object starting at the index Toffset, returns True
(2) Public boolean starwith (String prefix);
End of String method
public boolean endsWith (String suffix);
Vi. interception of sub-strings
(1) Public String subString (int beginindex);
(2) public string subString (int beginindex,int endindex);/string returned from Beginindex to EndIndex-1
To return 4 digits can be written like this Syetem.out.println (*.substring () (*.length ()-4));
Vii. Replacement of strings
Two different methods
(1) Public String replace (char Oldchar,char Newchar);
(2) public string replace (charsequence target,charsequence replacement);//Replace the original Etarget subsequence with the replacement sequence and return the new string
(3) public string ReplaceAll (string regex,string replacement);//to match a string with a regular expression
Viii. uppercase and lowercase conversions of strings
(1) Public String tolowercase (Locale Locale);
(2) Public String tolowercase ();
(3) Public String touppercase (Locale Locale);
(4) Public String touppercase ();
Nine, remove the end of the string space
*.trim ();
Ten, String conversion
1. Convert a string into a character array
Public char[] ToCharArray ();
2. Convert a string to an array of strings
Public string[] Split (String regex);//regex is a given match
3. Convert other data types to strings
(1) public static String valueof (Boolean B);
(2) public static String valueof (char c);
(3) public static String valueof (int i);
(4) public static String valueof (long i);
(5) public static String valueof (float f);
(6) public static String valueof (double D);
(7) public static String valueof (char[] data);
(8) public static String valueof (Object obj);
Creation and initialization of variable strings
Two ways:
public StringBuffer ();
Public stringbuffer (int caoacity);
Use of the main methods of the StringBuffer class:
First, get the variable string length
(1) public int length ();
(2) public int capacity ();
(3) public void SetLength (int newlength);
Second, compare variable strings
Compared with the Equals () method of the string class, but different.
The Equals () method in class object compares the addresses of two objects to be equal, not just the content, but the string class overrides the Equals () method when inheriting the object class, only comparing the contents of two objects to equal
The Equals () method of the object class is not overridden in the StringBuffer class, so the address and content are compared.
Third, append and insert strings
(1) Append public stringbuffer append (type T);
(2) Insert public stringbuffer Insert (int offset,type t);//Add a string of type to the offset
Iv. Reversing and deleting strings
(1) Inversion of public stringbuffer reverse ();
(2) Delete public stringbuffer Delete (int start,int end);
V. Reduce storage space for variable character sequences
public void TrimToSize ();
Six, StringBuffer class to String class
Public String toString ();