1.String class
(1) String Object
The ① string is a special object that cannot be changed once the string is initialized.
The ② string buffer supports variable strings. Because the String object is immutable, you can share
The Equals () method in the ③string class copies the contents of the object class, rather than comparing the object reference with the string content.
Public classStringdemo { Public Static voidMain (string[] args) {//TODO auto-generated Method StubStringDemo2 (); StringDemo1 (); } /*** Demonstrates the second way to define a string*/ Private Static voidStringDemo2 () {//TODO auto-generated Method StubString s= "ABC";//Create a String object in a constant poolString S1=NewString ("abc");//Create two objects, one new one in heap memorySystem.out.println (S==S1); System.out.println (S.equals (s)); } /*** Demonstrates the first way to define a string*/ Private Static voidStringDemo1 () {String s= "ABC"; String S1= "ABC"; System.out.println (S==S1); }}
(2) Constructors
String (byte[] bytes)
Constructs a new String by using the platform's default character set to decode the specified byte array.
String (int[] codepoints, int offset, int count)
Assigns a new String that contains the Unicode code points Group parameter to the character of a sub-array
Public classStringconstructordemo { Public Static voidMain (string[] args) {//TODO auto-generated Method StubStringConstructorDemo1 (); } Private Static voidStringConstructorDemo1 () {//TODO auto-generated Method Stub Char[] arr= {' A ', ' B ', ' C ', ' d '}; String s=NewString (arr,1,3); System.out.println ("S1=" +s); } Public Static voidStringconstructordemo () {//TODO auto-generated Method StubString s=NewString ();//not equal to string s=null; byte[] arr= {65,66,67}; String S1=NewString (arr); System.out.println ("S1=" +S1); }}
(3) Common methods of the string class
① get
1.1 Get string length int length ()
1.2 Get character char charAt (int index) based on position
1.3 Get the first occurrence of the position according to the character
int indexOf (int ch)
Gets the position of the first occurrence from the specified position
int indexOf (int ch,int fromIndex)
Returns the index at which the specified substring first appears in this string.
IndexOf (String str)
Returns the index of the first occurrence of the specified substring in this string, starting at the specified index
int indexOf (String str, int fromIndex)
Returns the index of the specified character at the last occurrence in this string.
int lastIndexOf (int ch)
Returns the index of the last occurrence of the specified character in this string, starting at the specified index to reverse-search.
int lastIndexOf (int ch, int fromIndex)
Returns the index of the rightmost occurrence of the specified substring in this string.
int lastIndexOf (String str)
Returns the index of the last occurrence of the specified substring in this string, starting at the specified index to reverse the search.
int lastIndexOf (String str, int fromIndex)
Note: You can determine whether a character or a string exists based on 1
1.4 Getting substrings of a string
String substring (int beginindex)
Returns a new string that is a substring of this string.
String substring (int beginindex, int endIndex)
Returns a new string that is a substring of this string.
② Conversion
2.1 Cutting a string into a string array
String[] Split (String regex)
Splits this string according to the match of the given regular expression.
String[] Split (String regex, int limit)
Splits this string by matching a given regular expression
2.2 Turning a string into a character array
Char[] ToCharArray ()
2.3 Changing a string into a byte array
Byte[] GetBytes ()
2.4 Convert letters in a string to uppercase
String toUpperCase ()
2.5 Replacing the contents of a string
String replace (char OldChar, char Newchar)
2.6 Remove both ends of a string
String trim ();
2.7 to concatenate a string
String concat ();
③ judgment
3.1 Two string content is the same?
Boolean equals (Object anobject)
Compares this string to another string, regardless of case
Boolean equalsignorecase (String anotherstring)
3.2 Does the string contain a string?
Boolean contains (Charsequence s)
3.3 Whether the string begins or ends with the specified string
Boolean startsWith (String prefix)
Boolean endsWith (String suffix)
④ comparison
int CompareTo (String anotherstring)
Compares two strings in a dictionary order.
int comparetoignorecase (String str)
Compares two strings in a dictionary order, regardless of case
2.StringBuffer class
StringBuffer: Character buffers, containers for storing data
(1) Features
① length Variable
② can store different types of data
③ will eventually be converted into strings for use
④ can modify the string
(2) function
① add
StringBuffer append (data);
② Insertion
StringBuffer Insert (Index,data);
③ Delete
StringBuffer Delete (start,end);//contains header, does not contain tail
StringBuffer deletecharat (int index);
④ Find
Char charAt (Index)
int IndexOf (String)
int LastIndexOf (String)
⑤ modification
StringBuffer Replace (start,end,string)
void Setcharat (Index,char)
3.StringBuilder class
(1) StringBuilder is a thread that is not synchronized. Typically used for single threading, high efficiency.
(2) The StringBuffer is thread-synchronized. Typically used for multithreading.
Class 4.String Exercises
/*1. Given an array of strings, sort from small to large in dictionary order. * * * *. The number of occurrences of a substring in the entire string. * Two the largest identical substring in a string. * * *. Simulates a consistent approach to trim functionality. */ Public classStringtest { Public Static voidMain (string[] args) {//TODO auto-generated Method Stub//1. Given an array of strings, sort from small to large in dictionary order. String[] arr= {"NBA", "abc", "CBA", "ZZ", "QQ", "haha"}; Sort (arr); //2. The number of occurrences of a substring in the entire string. String str= "Nbaernbatynbauinbaopnba"; String Key= "NBA"; System.out.println ("count==" +Getkeystringcount (Str,key)); //3. The largest of the same substrings in two strings. String maxsub=getmaxsubstring ("Qwerabcdeyuion", "QWERABCDE"); System.out.println ("Maxsub=" +maxsub); //4. Simulate a consistent approach to trim functionalityString s= "ABC abc"; System.out.println ("Trimsub=" +Mytrim (s)); } Public Static voidPrintarr (string[] arr) {//TODO auto-generated Method Stub for(inti=0;i<arr.length;i++) {System.out.print (Arr[i]+","); } } Public Static voidsort (string[] arr) {System.out.println ("Before sorting:"); Printarr (arr); for(inti=0;i<arr.length-1;i++) { for(intj=i+1;j<arr.length;j++) { if(Arr[i].compareto (arr[j]) >0) {swap (I,j,arr); }}} System.out.println (); System.out.println ("After sorting:"); Printarr (arr); System.out.println (); } Public Static voidSwapintIintJ, string[] arr) { //TODO auto-generated Method StubString temp=Arr[i]; Arr[i]=Arr[j]; ARR[J]=temp; } Public Static intGetkeystringcount (String str,string key) {intCount=0; intIndex=0; while((Index=str.indexof (key,index))!=-1) {Index=index+key.length (); Count++; } returncount; } Public Staticstring getmaxsubstring (string s1,string s2) {string Max= (S1.length () >s2.length ())?s1:s2; String min= (Max.equals (S1))?s2:s1; System.out.println ("Max=" +max); System.out.println ("Min=" +min); /**ABCDEFG 7-0 *abcdef bcdefg 7-1 *abcde bcdef cdefg 7-2 * ......... ........ */ for(intI=min.length (); i>0;i--) { for(intJ=0;j<=min.length ()-i;j++) {String Sub=min.substring (J, j+i); if(Max.contains (sub))returnSub; } } return NULL; } Public Staticstring Mytrim (string s) {intStart=0,end=s.length ()-1; while((Start<s.length ()-1) && (S.charat (start) = = ") ) {Start++; } while(End>0&&s.charat (end) = = ") {End--; } returnS.substring (Start, end+1); }}
017_ common Api_string class and StringBuffer class