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

Java 8Object Oriented ProgrammingProgramming

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
raja
Published on 14-Dec-2018 06:38:15
Advertisements