String类
一、简介
java.lang.String
java用于管理字符串的类
二、常用方法
1.构造方法
String(char value[])
分配一个新的String,以便它表示当前字符数组参数value包含的字符。新的String复制字符数组参数的内容。也就是说后续更改字符数组并不影响新的String。
/**
* Allocates a new {@code String} so that it represents the sequence of
* characters currently contained in the character array argument. The
* contents of the character array are copied; subsequent modification of
* the character array does not affect the newly created string.
*
* @param value
* The initial value of the string
*/
public String(char value[]) {
this.value = Arrays.copyOf(value, value.length);
}
2. String.tocharArray()
将此字符串转换成新的字符数组。
/**
* Converts this string to a new character array.
*
* @return a newly allocated character array whose length is the length
* of this string and whose contents are initialized to contain
* the character sequence represented by this string.
*/
public char[] toCharArray() {
// Cannot use Arrays.copyOf because of class initialization order issues
char result[] = new char[value.length];
System.arraycopy(value, 0, result, 0, value.length);
return result;
}