Both of these constructors make copies of the array, so you can change the array contents after you have
created a String from it without affecting the contents of the String.
For example, the following simple algorithm squeezes out all occurrences of a character from a string:
public static String squeezeOut(String from, char toss) {
char[] chars = from.toCharArray();
int len = chars.length;
int put = 0;
for (int i = 0; i < len; i++)
if (chars[i] != toss)
chars[put++] = chars[i];
return new String(chars, 0, put);
}
The method squeezeOut first converts its input string from into a character array using the method
toCharArray. It then sets up put, which will be the next position into which to put a character. After that
it loops, copying into the array any character that isn't a toss character. When the method is finished looping
over the array, it returns a new String object that contains the squeezed string.
You can use the two static String.copyValueOf methods instead of the constructors if you prefer. For
instance, squeezeOut could have been ended with
return String.copyValueOf(chars, 0, put);
There is also a single-argument form of copyValueOf that copies the entire array. For completeness, two
static valueOf methods are also equivalent to the two String constructors.
The toCharArray method is simple and sufficient for most needs. When you need more control over
copying pieces of a string into a character array, you can use the getChars method:
public voidgetChars(int srcBegin, int srcEnd, char[] dst, int
dstBegin)
Copies characters from this String into the specified array. The characters
of the specified substring are copied into the character array, starting at
dst[dstBegin]. The specified substring is the part of the string starting at
srcBegin, up to but not including srcEnd.
13.2.7. Strings and byte Arrays
Strings represent characters encoded as char values with the UTF-16 encoding format. To convert those
char values into raw byte values requires that another encoding format be used. Similarly, to convert
individual "characters" or arrays of raw 8-bit "characters" into char values requires that the encoding format
of the raw bytes is known. For example, you would convert an array of ASCII or Latin-1 bytes to Unicode
characters simply by setting the high bits to zero, but that would not work for other 8-bit character set
encodings such as those for Hebrew. Different character sets are discussed shortly. In the following
constructors and methods, you can name a character set encoding or use the user's or platform's default
encoding:
publicString(byte[] bytes, int start, int count)