Java中String, byte[], char[],StringBuffer, StringBuilder的区别联系与常见操作

文章目录

  • byte和char的区别
  • String、byte[]和char[]的联系
    • String的构造函数
      • byte[]转String
      • char[]转String
    • String转byte[]
    • String转char[]
  • String,StringBuilder,StringBuffer的区别与联系
    • String转StringBuffer和StringBuilder
    • StringBuffer转String
    • StringBuilder转String
  • String常见操作
  • 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.

你可能感兴趣的:(Java)