1. 程式人生 > >Java中String, byte[], char[],StringBuffer, StringBuilder的區別聯絡與常見操作

Java中String, byte[], char[],StringBuffer, StringBuilder的區別聯絡與常見操作

文章目錄

byte和char的區別

byte是位元組資料型別、有符號型的、佔1個位元組、大小範圍為-128——127
char是字元資料型別、無符號型的、佔2個位元組(unicode碼)、大小範圍為0-65535
byte和char在指定編碼型別的情況下,可以相互轉換

String、byte[]和char[]的聯絡

String的建構函式

byte[]轉String

String(byte[] bytes)
Constructs a new String by decoding the specified array of bytes using the platform’s default charset.
String(byte[] bytes, Charset charset)


Constructs a new String by decoding the specified array of bytes using the specified charset.
String(byte[] bytes, int offset, int length)
Constructs a new String by decoding the specified subarray of bytes using the platform’s default charset.
String(byte[] bytes, int offset, int length, Charset charset)

Constructs a new String by decoding the specified subarray of bytes using the specified charset.
String(byte[] bytes, int offset, int length, String charsetName)
Constructs a new String by decoding the specified subarray of bytes using the specified charset.
String(byte[] bytes, String charsetName)
Constructs a new String by decoding the specified array of bytes using the specified charset.

char[]轉String

與byte[]不同,char[]轉String不用指定編碼型別
String(char[] value)
Allocates a new String so that it represents the sequence of characters currently contained in the character array argument.
String(char[] value, int offset, int count)
Allocates a new String that contains characters from a subarray of the character array argument.

String轉byte[]

需指定編碼型別,預設使用平臺的預設編碼
byte[] getBytes()
Encodes this String into a sequence of bytes using the platform’s default charset, storing the result into a new byte array.
byte[] getBytes(Charset charset)
Encodes this String into a sequence of bytes using the given charset, storing the result into a new byte array.
byte[] getBytes(String charsetName)
Encodes this String into a sequence of bytes using the named charset, storing the result into a new byte array.

String轉char[]

char[] toCharArray()
Converts this string to a new character array.

String,StringBuilder,StringBuffer的區別與聯絡

String不可變,後兩者可變。StringBuffer是執行緒安全的,而StringBuilder不是。

String轉StringBuffer和StringBuilder

StringBuffer(String str)
Constructs a string buffer initialized to the contents of the specified string.
StringBuilder(String str)
Constructs a string builder initialized to the contents of the specified string.

StringBuffer轉String

String(StringBuffer buffer)
Allocates a new string that contains the sequence of characters currently contained in the string buffer argument.
String substring(int start)
Returns a new String that contains a subsequence of characters currently contained in this character sequence.
String substring(int start, int end)
Returns a new String that contains a subsequence of characters currently contained in this sequence.
String toString()
Returns a string representing the data in this sequence.

StringBuilder轉String

String(StringBuilder builder)
Allocates a new string that contains the sequence of characters currently contained in the string builder argument.
String substring(int start)
Returns a new String that contains a subsequence of characters currently contained in this character sequence.
String substring(int start, int end)
Returns a new String that contains a subsequence of characters currently contained in this sequence.
String toString()
Returns a string representing the data in this sequence.

String常見操作


  • String replace(char oldChar, char newChar)
    Returns a string resulting from replacing all occurrences of oldChar in this string with newChar.
    String replace(CharSequence target, CharSequence replacement)
    Replaces each substring of this string that matches the literal target sequence with the specified literal replacement sequence.
    String replaceAll(String regex, String replacement)
    Replaces each substring of this string that matches the given regular expression with the given replacement.
    String toLowerCase()
    Converts all of the characters in this String to lower case using the rules of the default locale.


  • char charAt(int index)
    Returns the char value at the specified index.
    boolean endsWith(String suffix)
    Tests if this string ends with the specified suffix.
    boolean startsWith(String prefix)
    Tests if this string starts with the specified prefix.
    int indexOf(int ch)
    Returns the index within this string of the first occurrence of the specified character.-1 if the character does not occur.
    int indexOf(String str)
    Returns the index within this string of the first occurrence of the specified substring.
    int indexOf(String str, int fromIndex)
    Returns the index within this string of the first occurrence of the specified substring, starting at the specified index.

  • 比較
    boolean contains(CharSequence s)
    Returns true if and only if this string contains the specified sequence of char values.
    boolean contentEquals(CharSequence cs)
    Compares this string to the specified CharSequence.
    boolean contentEquals(StringBuffer sb)
    Compares this string to the specified StringBuffer.
    boolean equals(Object anObject)
    Compares this string to the specified object.
    boolean equalsIgnoreCase(String anotherString)
    Compares this String to another String, ignoring case considerations.

  • 拼接
    String concat(String str)
    Concatenates the specified string to the end of this string.

  • 擷取
    String substring(int beginIndex)
    Returns a string that is a substring of this string.
    String substring(int beginIndex, int endIndex)
    Returns a string that is a substring of this string.

StringBuilder常見操作


  • StringBuilder append(char c)
    Appends the string representation of the char argument to this sequence.
    StringBuilder append(char[] str)
    Appends the string representation of the char array argument to this sequence.
    StringBuilder append(char[] str, int offset, int len)
    Appends the string representation of a subarray of the char array argument to this sequence.
    StringBuilder append(CharSequence s)
    Appends the specified character sequence to this Appendable.
    StringBuilder append(CharSequence s, int start, int end)
    Appends a subsequence of the specified CharSequence to this sequence.
    StringBuilder append(基本資料型別)
    “abc”+false -> abcfalse

  • StringBuilder delete(int start, int end)
    Removes the characters in a substring of this sequence.
    StringBuilder deleteCharAt(int index)
    Removes the char at the specified position in this sequence.

  • void setCharAt(int index, char ch)
    The character at the specified index is set to ch.
    StringBuilder replace(int start, int end, String str)
    Replaces the characters in a substring of this sequence with characters in the specified String.
    StringBuilder reverse()
    Causes this character sequence to be replaced by the reverse of the sequence.
  • 插入
    StringBuilder insert(int offset, char c)
    Inserts the string representation of the char argument into this sequence.
    StringBuilder insert(int index, char[] str, int offset, int len)
    Inserts the string representation of a subarray of the str array argument into this sequence.
    StringBuilder insert(int offset, int i)
    Inserts the string representation of the second int argument into this sequence.
    StringBuilder insert(int offset, Object obj)
    Inserts the string representation of the Object argument into this character sequence.
    StringBuilder insert(int offset, String str)
    Inserts the string into this character sequence

  • int indexOf(String str)
    Returns the index within this string of the first occurrence of the specified substring.
    int indexOf(String str, int fromIndex)
    Returns the index within this string of the first occurrence of the specified substring, starting at the specified index.
  • 子串
    CharSequence subSequence(int start, int end)
    Returns a new character sequence that is a subsequence of this sequence.
    String substring(int start)
    Returns a new String that contains a subsequence of characters currently contained in this character sequence.
    String substring(int start, int end)
    Returns a new String that contains a subsequence of characters currently contained in this sequence.