Creating a string from a subset of the array elements in Java


To get a string from a subset of the character array elements, use the copyValueOf() method. This method returns a String that represents the character sequence in the array specified.

Here is our character array.

char[] ch = { 'T', 'E', 'S', 'T', 'I', 'N', 'G'};

Now, let us create a string from the subset of the above array elements.

String str = String.copyValueOf(ch, 4, 2);

Example

 Live Demo

public class Demo {
   public static void main(String[] args) {
      char[] ch = { 'T', 'E', 'S', 'T', 'I', 'N', 'G'};
      String str = String.copyValueOf(ch, 4, 2);
      System.out.println(str);
   }
}

Output

IN

Samual Sam
Samual Sam

Learning faster. Every day.

Updated on: 27-Jun-2020

301 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements