Creating String Object from certain part of a character Array in Java


Here is our character array.

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

Create string object from some part of a string using the following String constructor. Through this we are fetching substring “IN” from the character array.

String str = new String(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 = new String(ch, 4, 2);
      System.out.println(str);
   }
}

Output

IN

karthikeya Boyini
karthikeya Boyini

I love programming (: That's all I know

Updated on: 27-Jun-2020

69 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements