Sring StringBuffer StringBuilder package type

Sring StringBuffer StringBuilder package type

First, using a common method of the String class

String data is an object

Once the initialization character string data can not be changed

String object are stored in the constant pool, the string constant pool

== comparison is the address value is re-opened and new

1, Analyzing
  • the equals (Object obj) : Object string class overrides the method for comparing the value of the string are equal
    • equalsIfnoreCase (String anotherString) values for the standard plastic strings are equal case-insensitive
  • the contains (S String) : determines whether a string contains another string
  • With startd (String prefix) : Test times whether a string is specified prefix beginning
  • endsWith (String suffix): Tests if this string ends with the specified suffix
  • Whether idEmpty () is determined string is empty
2, get
  • int length (): Get string length

  • char charAt (int index): Gets the character at the specified position

  • Get the specified index of the first character string or strings appear in a given string

    • int indexOf (int ch) Gets the specified string in the index of the first occurrence of the string
    • int indexOf (int ch, int fromIndex) Returns the index at a specified character appears in this string for the first time, start the search from the specified index.
    • int indexOf (String str) Gets the index of the specified string at the first occurrence of a string
    • int indexOf (String str, int fromIndex) Returns the index at a specified character appears in this string for the first time, start the search from the specified index.
  • Gets the specified character or string in a given string index of the last occurrence

    int lastIndexOf (int CH) Returns the specified character index within this string of the last occurrence.

    int lastIndexOf (CH int, int fromIndex) Returns the index within this string of characters at the last occurrence,

    Start reverse searching from the specified index.

    int lastIndexOf (String str) Returns the specified substring in this string of the rightmost occurrence index.

    int lastIndexOf (String STR, int fromIndex) Returns the substring in this string at the time of the last occurrence

    The index, starting at the specified index reverse lookup.

  • Get a substring

    String substring (int the beginIndex) Returns a new string that is a substring of this string.

    String substring (int the beginIndex, int endIndex) Returns a new string that is a string of

    Substring. < -Containing free end of the head >

3, conversion
  • String and a byte array conversion

    From String to byte [] ------> byte [] the getBytes ()

    From byte [] to -------- String> new new String (byte [] by) constructor .

  • Convert a string and an array of characters

    From String to char []

    char[] toCharArray()

    length (), charAt (int index) binding

    From char [] to String

    new String(char[] ch)

    static copyValueOf(char[] data)

    static String valueOf(char[] data, int offset, int count)

  • String valueOf static (Y XXX) XXX basic data types

    For example: int x = 4;

    The basic data types into a string: String.valueOf (X) ;

  • static String valueOf (Object obj) the object into a string

    For example:

    Demo d = new Demo();

    sop(d);

    sop(d.toString());

    sop(String.valueOf(d));

  • String toUpperCase () all characters are converted to uppercase

    String toLowerCase () All characters are converted to lowercase

  • Connection string

    String concatenation operators: +

    String concat (String str) the specified string is connected to this end of the string.

    String s = "aaa" s.concat("bbb").concat("ccc").concat("ddd");

4. Replace
  • String Replace (char occurrences of oldChar in, newChar char) Returns a new string, which is obtained by using for newChar

    All oldChar change occurs in this string obtained.

    String Replace (oldString String, String newString) Returns a new string, which is obtained by replacing all oldString this string occurs with newString obtained.

5, cutting
  • String [] Split (String REGEX) dividing the given string string.
6, remove the string space
  • trim () remove the string and trailing spaces

三、StringBuffer&StringBuilder

  • String String is a fixed length, StringBuffer variable length

  • Additional elements

    • insert (index, element)
    • Reverse (); Reverse
    • The number of elements that can store capacity; () Capacity
    • It actually stores the number of longitudinal elements; () length
    • String substring (index); StringBuffer in the copy part, then the copy assigned to this part of a String.
  • StringBuffer,StringBuilder区别

    • StringBuffer thread-safe, low efficiency (synchronous)
    • StringBuilder thread unsafe, inefficient (not synchronized)
    • The basic data types are compared with comparison operators, comparison reference data type compare To

Guess you like

Origin www.cnblogs.com/XtsLife/p/11059637.html