Java 中 String、StringBuffer、StringBuilder 类

A, String class

  1. Information

  Nature: the nature of the string is an array of characters.

    S3 = String  "ABC" and  String S4 =  new new  String ( "ABC" distinction); and

    String s3 = "abc":

        There is heap constant pool in the form of an array of characters, assigning addresses in the constant pool.

    String s4 = new String("abc"):

        First appeared in the constant pool, address allocation, and then open up the heap space, and the space assigned for this purpose, assign addresses, the address space is stored in the constant pool.

    2, constructors

    String str = new String (byte [] bytes): change the parameter byte array, corresponding to the ASCII code table.

      String str = new String (byte [] bytes, int offset, int length): The first parameter: array of bytes, the second parameter: the incoming byte array start index, the third parameter: the incoming byte array index position, how many interception.

      String str = new String (char [] chars) :: character array parameter is changed.

      String str = new String (char [] char, int offset, int length): The first parameter: character array, the second argument: start index incoming character array, the third parameter: the incoming character array index position , how many interception.

    3, commonly used method

      length (): Get the length of the string. Returns an int

      the substring (int startIndex, [intEndIndex] ): interception string, the first parameter: Start Capture index string, the second argument: Do not write, finally intercepted, taken to write the index position (no header trailer ). Returns a String

     startsWith (String str): determining if the string is returned to the start of the specified substring Boolean

     endsWith (String str): determining if the string is returned to the start of the specified substring Boolean

     contains (String str): Analyzing small Large string comprising the string is not returned Boolean

     indexOf (String str): determining the index position of a small character string appears in the character string of the large return int 

     getBytes (): Use: byte [] bytes = str.getBytes (); string specified byte array transfer

     toCharArray (): Use: char [] chars = str.toCharArray (); transfer the specified string character array

     equals (Object obj): Analyzing the content of the two strings is not returned as Boolean;

     equalsIgnoreCase (Object obj): Analyzing the content of the two strings is not the same, but a case-insensitive return Boolean;

  Two, StringBuffer class

   Variable string:

    1, StringBuffer and String conversion;  

      String 转 StringBuffer :StringBuffer sb = new StringBuffer(String str);

      StringBuffer 转 String :String str = sb.toString();

     2, a common method

    append (Object obj): Behind the specified string is added.

      delete (int startIndex, int endIndex): delete the specified index string.  

      insert (int Index, String str): specified string is inserted at the specified location index

      replace (int startIndex, int endIndex, String str): specified string replaces the character string specified index region

     reverse (): string reversed

    The above method returns the type of the object are StringBuffer chain can be programmed

    

  StringBuilder class: StringBuffer class and have the same method, but efficiency due StringBuffer 

 

 

Guess you like

Origin www.cnblogs.com/yanghaoyu0624/p/11577131.html