×

Java String copyValueOf() method

copyValueOf() method returns a String that holds the character sequence of the character array.

Syntax:

copyValueOf(char[] data)

Parameters:

data : the character array i.e. String

Returns:

It returns a String that contains the characters of the character array.

Java String copyValueOf() Example 1:

public class JavaCopyValueOfEx1 {
   public static void main(String args[]) {
       char[] data = {'a','b','c','d','e','f','g','h','i','j','k'};
       String str1 = "Text";   
       str1 = str1.copyValueOf(data);
       System.out.println("str1 after copy: " + str1);
   }
}

Output:

str1 after copy: abcdefghijk

Java String copyValueOf() Example 2:

public class JavaCopyValueOfEx2 {
   public static void main(String args[]) {
       char[] data = {'a','b','c','d','e','f','g','h','i','j','k'};
       String str1 = "Text";
       String str2 = "String";
       //Variation 1:String copyValueOf(char[] data)
       str1 = str1.copyValueOf(data);
       System.out.println("str1 after copy: " + str1);
       //Variation 2:String copyValueOf(char[] data,int offset,int count)
       str2 = str2.copyValueOf(data, 5, 3 );
       System.out.println("str2 after copy: " + str2);
   }
}

Output:

str1 after copy: abcdefghijkstr2 after copy: fgh

Java String copyValueOf() Example 3:

public class JavaCopyValueOfEx3
{
public static void main(String[] args)
    {
        // Character array with data.
char[] arr_num = new char[] { '1', '2', '3', '4', '5', '6', '7', '8', '9' };
        // Create a String containig the contents of arr_num
        // starting at index 1 for length 2.
        String str = String.copyValueOf(arr_num, 4, 3);
        // Display the results of the new String.
System.out.println("\nThis Java Book contains  " + str +" pages.\n");
    }
}

Output:

This Java Book contains  567 pages.

Java String copyValueOf() Example 4:

public class JavaCopyValueOfEx4 {
  public static void main(String args[]) {
       // Initialising Character array
     char[] ch = {'R', 'O', 'Y', ' ', 'N', 'I', 'T', 'I', 'S', 'H'};
      // Initialising String
     String ch2 = "";    
     // Copying value in ch2
     // now ch2 is equal to "ROY NITISH"
     ch2 = ch2.copyValueOf( ch );     
     // Printing String
     System.out.println("copied string is : " + ch2);
  }
}

Output:

copied string is : ROY NITISH

Related Topics

Java Program to Create Set of Pairs Using HashSet

HashSet in Java: HashSet is an assortment in Java that has a place with java.util bundle. It inside utilises HashMap to store components. It is an execution of the Set connection...

11 minutes read.

Difference Between Access Specifiers and Modifiers in Java

Java employs access modifiers to restrict a class's data members, member functions, and constructor. Access modifiers are essential when creating Java program and applications. Access modifiers in Java include: defaultpublicprotectedprivate Default Access Modifiers Without...

4 minutes read.

How to Convert String to char in Java

How to Convert String to char in Java There are two methods to convert String to char are: Using charAt() method Using tocharArray() method Using charAt() method This is the method of String class that...

3 minutes read.

Java Class Name

How to write a class name? The following considerations should be made while writing class names. The name of the current class shouldn't be based on a preset or existing class. Java keywords...

3 minutes read.

Stack Program in Java

Stack Program in Java The Stack class is part of the collection framework that inherits the Vector class. Thus, the Stack class can also be called a subclass of the Vector...

5 minutes read.

Longest Odd Even Subsequence in Java

In order to solve the Java problem known as the longest odd-even subsequence, one must identify a sequences in a non-negative array having size s that alternately includes odd and...

6 minutes read.

Difference between this and super in Java

In this article, you will be very well equipped with the knowledge of this keyword and super keyword in java. You will be able to understand where to implement this...

3 minutes read.

Java Imageio

The javax.imageio package contains the final class known as Java ImageIO. For easy image reading, writing, and simple encoding and decoding, the class offers a convenient way. The class offers...

4 minutes read.

Check whether a Number is a Power of 4 or Not in Java

There are many ways to figure out if an integer is a power of 4. This section will go over a variety of techniques for figuring out whether or not...

11 minutes read.

Best Java Security Framework

The security of applications is currently our top concern when creating them. The applications or bits of code running over the network are exposed to dangers and may jeopardize integrity,...

3 minutes read.

Java Plot

Java Plot is a phrase in Java that is mostly used for plotting coordinates on a cartesian plane. Plotting graphs in Java is accomplished through the use of various core...

3 minutes read.

JAR File in Java

What is a JAR File? JAR stands for Java Archive. It is a file format mainly used to combine many Java class files and the corresponding metadata and resources into one...

4 minutes read.

Int vs Integer in Java

In Java, numerical data can be stored using int or Integer. int and Integer both have different definitions but similar usage in Java. What is int in Java? int is a primitive...

4 minutes read.

Hashing Algorithm in Java

The hashing algorithm is a method that maps data to the fixed-length hash. The Java hash-based algorithm employs a cryptographic mathematical operation. A hash technique or hash function is supposed...

8 minutes read.

Java String Matches vs Contains

String Matches in Java The matches() function and its variations are used to determine whether or not a provided text matches a regular expression. The functioning as well as output of...

3 minutes read.

Java Queue Interface

Queue interface is a subtype of Collection interface. All methods in the Collection interface are also available in the Queue interface. It provides operations of Collection and also some additional...

2 minutes read.

Java Math expm1() Method

The expm1() method of Math class returns ex-1 where e represents Euler’s number. Syntax: public static double expm1(double x) Parameters: The parameter ‘x’ represents the exponent to raise e in the calculation of ex-1. Return...

2 minutes read.

Modules in Golang

Modules are a way to manage dependency versions and enable reproducible builds of Go programs. They were introduced in Go 1.11 and are now the recommended way to manage dependencies...

4 minutes read.

Java BufferedWriter

BufferWriter Class: It is used to write the data more efficiently. This class is present in the java.io package, it inherits the data from the Writer class. Writer class is...

4 minutes read.

How to run java program in ubuntu

To run the java programming in ubuntu, we need to follow several steps: Let's see the process. Step1: Install the Java compiler or JDK to the system. To run the java program, we...

3 minutes read.