Java String getChars() method example.

Java 8Object Oriented ProgrammingProgramming

The getChars() method of a String class accepts four parameters namely −

  •  srcBegin − index of the first character in the string to copy.
  •  srcEnd − index after the last character in the string to copy.
  •  dst − the destination array.
  •  dstBegin − the start offset in the destination array.

Example

It copies all the characters of the current string to the destination character array.

Live Demo

import java.io.*;
public class Test {
   public static void main(String args[]) {
      String Str1 = new String("Welcome to Tutorialspoint.com");
      char[] Str2 = new char[7];
      try {
         Str1.getChars(2, 9, Str2, 0);
         System.out.print("Copied Value = " );
         System.out.println(Str2 );
      } catch ( Exception ex) {
         System.out.println("Raised exception...");
      }
   }
}

Output

Copied Value = lcome t
raja
Published on 12-Feb-2018 06:18:19
Advertisements