The Java copyValueOf Method is one of the Java String Method which is used to return the string that represents the character sequence in the specified array.
In this article we will show you, How to use String copyValueOf in Java Programming language with example. Before we get into the example, the basic syntax of the string.copyValueOf in Java Programming language is as shown below.
Java copyValueOf Method syntax
The Java Programming Language provides two different methods to return the string representation.
The following method will accept the character array as the parameter and return a string that represent the characters in an array.
public static String copyValueOf(char[] anCharArray); // It will return String //In order to use in program String.copyValueOf(char[] anCharArray);
Following Java copyValueOf method will accept the character array as the first parameter, Starting index position (Starting_index) as the second argument and the array count as third argument.
The Java String.copyValueOf method will start copying values from the Starting_index and return upto user specified count. For example, char[] ch = {‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’} and String.copyValueOf(ch, 2, 4) will return the string as cde
public static String copyValueOf(char[] anCharArray, int Starting_index, int count); // It will return String //In order to use in program String.copyValueOf(char[] anCharArray, int Starting_Index, int count);
- Starting_Index: Please specify the starting index position. If you specify this value then, instead of copying from index position 0 String.copyValueOf function will start copying from this position.
- Count: Please specify the length of a sub array.
Java copyValueOf Method Example
The Java string copyValueOf method is used to return a string that represent the character sequence. This Java program will help you to understand the Java string.copyValueOf method.
JAVA CODE
//Java String.copyValueOf example package StringFunctions; public class CpyValueOfMethod { public static void main(String[] args) { char[] ch = {'a', 'b', 'c', 'd', 'e', 'f'}; System.out.println(String.copyValueOf(ch)); String str = "Hello " + String.copyValueOf(ch); System.out.println(str); String str1 ="Hi"; str1 = str1 + " Hello " + String.copyValueOf(ch); System.out.println(str1); } }
OUTPUT
ANALYSIS
First we declared an array of characters using following statement
char[] ch = {'a', 'b', 'c', 'd', 'e', 'f'};
Following statement will call the public static String copyValueOf (char[] anCharArray) method to return the string representation of the above specified character array ch
System.out.println(String.copyValueOf(ch));
Next, we are adding some string data to the copyValueOf function return value. It means Hello + abcdef
String str = "Hello " + String.copyValueOf(ch);
Java copyValueOf Method Example 2
The Java string copyValueOf method is used to return a string that represent the character sequence in the specified sub array. This Java program will help you to understand the Java string.copyValueOf method.
JAVA CODE
//Java String.copyValueOf example package StringFunctions; public class CopyValueOfMethod2 { public static void main(String[] args) { char[] ch = {'J', 'a', 'v', 'a', ' ', 'P', 'r', 'o', 'g', 'r', 'a', 'm'}; System.out.println(String.copyValueOf(ch, 1, 8)); String str = "Hello " + String.copyValueOf(ch, 0, 4); System.out.println(str); String str1 = "Hello " + String.copyValueOf(ch, 5, 7); System.out.println(str1); String str2 = "Hello " + String.copyValueOf(ch, 0, 4) + " Programming"; System.out.println(str2); } }
OUTPUT
ANALYSIS
Within this Java copyValueOf example, First we declared an array of characters using following statement
char[] ch = {'J', 'a', 'v', 'a', ' ', 'P', 'r', 'o', 'g', 'r', 'a', 'm'};
Following statement will call the public static String copyValueOf (char[] anCharArray, int Starting_Index, int Count) method to return the string representation of the above specified character array ch. This will start looking for character at index position 1 (i.e., a) and count 8 characters (i.e., a, v, a, , P, r, o, g), and then it will return the string representation of this sub array.
System.out.println(String.copyValueOf(ch, 1, 8));
Next, we are adding some string data to the copyValueOf method return value. It means Hello + character at index position 0 (i.e., J) and count 4 characters (i.e., J, a, v, a)
String str = "Hello " + String.copyValueOf(ch, 0, 4);
Thank You for Visiting Our Blog