beginnersbook.com

  • Home
  • All Tutorials
    • Learn Servlet
    • Learn JSP
    • Learn JSTL
    • Learn C
    • Learn C++
    • Learn MongoDB
    • Learn XML
    • Learn Python
    • Learn Perl
    • Learn Kotlin
    • Learn jQuery
  • Core Java
  • OOPs
  • Collections
  • Java I/O
  • JSON
  • DBMS

Java – String getChars() Method example

By Chaitanya Singh | Filed Under: String handling

The method getChars() is used for copying String characters to an Array of chars.

public void getChars(int srcBegin, int srcEnd, char[] dest, int destBegin)

Parameters description:
srcBegin – index of the first character in the string to copy.
srcEnd – index after the last character in the string to copy.
dest – Destination array of characters in which the characters from String gets copied.
destBegin – The index in Array starting from where the chars will be pushed into the Array.

It throws IndexOutOfBoundsException – If any of the following conditions occurs:
(srcBegin<0) srcBegin is less than zero. (srcBegin>srcEnd) srcBegin is greater than srcEnd.
(srcEnd > length of string) srcEnd is greater than the length of this string.
(destBegin<0) destBegin is negative.
dstBegin+(srcEnd-srcBegin) is larger than dest.length.

Example: getChars() method

public class GetCharsExample{
   public static void main(String args[]){
       String str = new String("This is a String Handling Tutorial");
       char[] array = new char[6];
       str.getChars(10, 16, array, 0);
       System.out.println("Array Content:" );
       for(char temp: array){
           System.out.print(temp);
       }

       char[] array2 = new char[]{'a','a','a','a','a','a','a','a'};
       str.getChars(10, 16, array2, 2);
       System.out.println("Second Array Content:" );
       for(char temp: array2){
    	   System.out.print(temp);
       }
   }	
}

Output:

Array Content:
StringSecond Array Content:
aaString

Comments

  1. E says

    November 21, 2016 at 1:10 PM

    Is there a typo or is Array Content not supposed to print anything?

    Reply
  2. E says

    November 21, 2016 at 1:15 PM

    Sorry, I see now why there is no output for Array Content! Is it because String is not an array ?

    Reply
  3. E says

    November 21, 2016 at 1:17 PM

    Correction: I see it’s because indices 10 and 16 give spaces!

    Reply
  4. E says

    November 21, 2016 at 1:20 PM

    Why does String print out in the secondarray content though? I realize there should probably be a new line in front of “Second Array Content” to avoid confusion.

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Java Tutorials

  • Learn Java
  • OOPs Concepts
  • Java Collections

Java String

  • Java String

Java String Methods

  • String charAt()
  • String compareTo()
  • String compareToIgnoreCase()
  • String contains()
  • String concat()
  • substring
  • String valueOf()
  • String startsWith()
  • String equals()
  • String format()
  • String endsWith()
  • String indexOf()
  • String lastIndexOf()
  • String length()
  • String replace()
  • String split()
  • String trim()
  • String intern()
  • String isEmpty()
  • String matches()
  • String regionMatches()
  • String contentEquals()
  • String toCharArray()
  • String getBytes()
  • String join()
  • String getChars()
  • String copyValueOf()

Recently Added..

  • JSON Tutorial
  • Java Regular Expressions Tutorial
  • Java Enum Tutorial
  • Java Annotations Tutorial

Copyright © 2012 – 2022 BeginnersBook . Privacy Policy . Sitemap