Wednesday 18 January 2017

Java String getChars() Method




The getChars() Method:

  This method is used to return more than one character at a time from a given string. These characters are copied into specified char array. This method required 4 arguments.
  The general form of getChars() method is given bellow:

public void getChars(int sourceStart, int sourceEnd, char[] target, int targetStart)

  Where sourceStart defines the index of the beginning of the sub string and sourceEnd defines the index of the end of the sub string. This sub string will be copied to the char array target at the position targetStart. The array target must be large enough to hold the character in the sub string.
  IndexOutOfBoundsException: It throws StringIndexOutOfBoundsException, if sourceStart index is greater than sourceEnd index.
  Consider the following examples:

public class GetCharsTest
{
  public static void main(String arg[])
  {
    String s="Java Programing";
    int start=5;
    int end=15;
    char[] target=new char[end-start];
    s.getChars(start, end, target, 0);
    System.out.println(target);
  }
}

Output is: Programing


0 comments:

Post a Comment

Powered by Blogger.

Stats