⬅ Previous Topic
Java StringcontentEquals()
methodNext Topic ⮕
Java StringendsWith()
methodcopyValueOf()
method⬅ Previous Topic
Java StringcontentEquals()
methodNext Topic ⮕
Java StringendsWith()
methodThe copyValueOf()
method in Java's String
class provides a convenient way to create a new string from a character array. It essentially copies a portion of the character array into a newly created String object. There are two overloaded versions:
public static String copyValueOf(char[] data)
public static String copyValueOf(char[] data, int offset, int count)
Parameter | Description |
---|---|
data |
The character array from which the String will be created. |
offset |
The index within data where copying begins. |
count |
The number of characters to copy from data , starting at the specified offset. |
A new String object containing a copy of the specified portion of the character array.
This example demonstrates how to create a String from a character array using the first version of copyValueOf()
, which copies all characters in the array.
char[] charArray = {'J', 'a', 'v', 'a'};
String str = String.copyValueOf(charArray);
System.out.println(str);
Java
Explanation: The charArray
holds the characters 'J', 'a', 'v', and 'a'. The copyValueOf()
method takes this array and creates a new String object containing those same characters, which is then printed to the console.
This example shows how to use the second version of copyValueOf()
. It extracts a portion of a character array starting at a given offset and copying a specified number of characters, creating a new String.
char[] charArray = {'H', 'e', 'l', 'l', 'o'};
String str = String.copyValueOf(charArray, 1, 3);
System.out.println(str);
ell
Explanation: Here, we're using an offset of 1 and a count of 3. This means the copying starts from the second character ('e') in the array and copies the next three characters ('l', 'l', 'o'). The resulting String is then printed to the console.
This example shows how the method behaves when passed an empty char array.
char[] charArray = {};
String str = String.copyValueOf(charArray);
System.out.println("|" + str + "|");
|
Explanation: When an empty character array is passed to copyValueOf()
, the method returns an empty String. The `println` statement outputs a pipe symbol on either side of the string to make it clear that an empty string has been created.
This example shows what happens when you give an out-of-bounds offset or count.
char[] charArray = {'A', 'B', 'C'};
String str1 = String.copyValueOf(charArray, 1, 5); // count exceeds array length
String str2 = String.copyValueOf(charArray, -1, 2); // Negative offset
System.out.println("|" + str1 + "|");
System.out.println("|" + str2 + "|");
|ABC|
Explanation: When the count
exceeds the boundaries of the array, or when a negative offset is provided, Java's copyValueOf()
method gracefully handles this. It copies up to the maximum possible number of characters that can be extracted given the array bounds.
⬅ Previous Topic
Java StringcontentEquals()
methodNext Topic ⮕
Java StringendsWith()
methodYou can support this website with a contribution of your choice.
When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.