Summary String, StringBuffer and StringBuilder class commonly used method

A commonly used method of String Class

. 1 . 1 . Get:
 2          . 1 ) acquires a length of string str
 . 3                  int I = str.length ();
 . 4          2 ) acquires the character based on the position (index)
 . 5                  char   C = str.charAt (index);
 . 6          . 3 ) acquired character string position
 . 7                  int I = str.indexOf ( char CH);   // Get the position of the first occurrence 
. 8                  int I = str.indexOf ( char CH, int index);   // Get the position index from the first occurrence of a position ch 
. 9                  int   I = str.indexOf (str1); // Get position str1 str first appears at 
10                 int I = str.indexOf (str1, index0); // Get position from the index position of the first occurrence of str 
. 11                  int I = str.lastIndexOf (ch or str1)   // position acquisition ch str1 last seen or 
12 is   
13 is 2 . Analyzing
 14          . 1 ) determines whether to start with the specified string str1, the end of the
 15                  Boolean B = str.startWith (str1)   // beginning 
16                  Boolean B = str.endsWith (str1) // end 
. 17          2 ) comprises determining whether a sub-string
 18 is                  Boolean B = str.contains (str1)
 . 19          . 3 ) determines whether the content string
 20 is                  Boolean B =str.isEmpty ();
 21 is          . 4 ) determines whether the character string identical to ignore case
 22 is                  Boolean B = str.equalsIgnoreCase (str1);
 23 is   
24 . 3 . converters
 25          1) character array - char [] CH-, converted into a string
 26              string STR = I. new new string (CH); // the whole array into a string 
27              II string = STR. new new string (CH, offset, count)
 28      // the count number of elements after the offset location of the character array into a string   
29              1. string STR = String.valueOf (CH);
 30              2. string STR =String.copyValueOf (CH, offset, COUNT);
 31 is              3. String STR = String.copyValueOf (CH);
 32          2 ) is converted to a character string array
 33 is              char [] = CH str.toCharAarray ();
 34 is          . 3 ) The byte array into a string
 35              above 1) incoming type to byte [];
 36          . 4 ) to convert the string into a byte array
 37 [              byte [] B = str.toByteArray ();
 38 is          . 5 ) the basic data types attached to a string
 39              string STR = String.valueOf (basic data types);
 40              if the character string data may be shaped connector + "" 
 41 is              EG: string = 5+ STR "";
 42              to obtain the string "5 "   
 43 is   
44 is . 4 . Alternatively Replace ();
 45          str.replace (oldChar, newChar) // str to changes in oldChar newChar 
46 is          str.replace (str1, str2) // in str str1 , becomes str2 
47   
48 . 5 . cutting split ();
 49          String [] str.split str1 = ( ""); // str to use "," String is divided into an array of 
50   
51 is . 6 . substring
 52 is          String S = str.substring (begin);
 53 is          // S str in position to begin from the last string 
54 is          string S =str.substring (begin, end)
 55          // S str is the string end from the position to begin location 
56 is   
57 is . 7 to change the case:.
 58          . the toUpperCase String str = S1 (); // str to capitalize letters 
59          S2 = the toLowerCase str String ();. // str to lowercase letters 
60      removed space:
 61 is          String S = str.trim ();
 62 is      comparison:
 63 is          int I = str.compareTo (str1);

Two, StringBuffer common method

. 1      / ** * the StringBuffer container is a variable length, the string can be directly manipulated, using the method becomes toString string * * / 
2 . 1 storage.
 . 3          . 1) the append (); // the specified data is added to the end of the container The return value is the StringBuffer 
. 4          EG:
 . 5          the StringBuffer SB = new new the StringBuffer ( // can be added STR); 
. 6          the StringBuffer SB1 = ab.append (data) // data may be any type of data base 
7      Note: At this point == SB SB1 them the same object, meaning that the new sb1 can not directly sb.append (data) after the bonding used when using sb
 . 8 2) iNSERT (); // insertion 
. 9      sb.insert (index, data);
 10 2 . remove
 11         sb.delete (start, end);   // delete the contents of the character start end of
 12  // NOTE: All operations are contained herein index containing free end of the head 
13 is          sb.deleteCharAt (index); // delete the specified the character position
 14  // empty StringBuffer buffer 
15          SB = new new StringBuffer ();
 16          sb.delete (0 , sb.length ());
 . 17 . 3 . Gets
 18 is      char C = sb.charAt (index); // Get characters on the index 
. 19      int I = sb.indexOf ( char ): // get char character appears first position
 20      // and methods consistent String Gets the front reference 
21  
22 is . 4 . No modification operations String class method
 23 is      SB = sb.replace (start, end, String) // will begin to start end of the replacement string String; 
24      sb.setCharAr (index, char ); / / character becomes the new index position char 
25   
26 is 5. the inversion sb.reverse (); // the reverse sb 
27 6. the getChars ( int the srcBegin, int the srcEnd, char [] CH, int chBegin)
 28  // the StringBuffer buffer specified data is stored into the specified array

Three, StringBuilder StringBuilder and StringBuffer methods and functions exactly the same just an early version (StringBuffer) are thread-safe, the discovery of the utilization of the same multi-threaded heap String data manipulation is very small, in order to improve efficiency in the future idk1.5 have StringBuilder class. Meaning is multi-threaded operating time with the same string StringBuffer security, are now generally used StringBuilder

Reference: https://blog.csdn.net/qq_34107571/article/details/80098055

Guess you like

Origin www.cnblogs.com/116970u/p/11495096.html