BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Java – String getChars() Method example

By Chaitanya Singh | Filed Under: java

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 Tutorial

Java Introduction

  • Java Index
  • Java Introduction
  • History of Java
  • Features of Java
  • C++ vs Java
  • JDK vs JRE vs JVM
  • JVM - Java Virtual Machine
  • First Java Program
  • Variables
  • Data Types
  • Operators

Java Flow Control

  • Java If-else
  • Java Switch-Case
  • Java For loop
  • Java while loop
  • Java do-while loop
  • Continue statement
  • break statement

Java Arrays

  • Java Arrays

OOPs Concepts

  • OOPs Concepts
  • Constructor
  • Java String
  • Static keyword
  • Inheritance
  • Types of inheritance
  • Aggregation
  • Association
  • Super Keyword
  • Method overloading
  • Method overriding
  • Overloading vs Overriding
  • Polymorphism
  • Types of polymorphism
  • Static and dynamic binding
  • Abstract class and methods
  • Interface
  • Abstract class vs interface
  • Encapsulation
  • Packages
  • Access modifiers
  • Garbage Collection
  • Inner classes
  • Static import
  • Static constructor

Java Exception Handling

  • Exception handling
  • Java try-catch
  • Java throw
  • Java throws
  • Checked and Unchecked Exceptions
  • Jav try catch finally
  • Exception Examples
  • Exception Propagation

Collections Framework

  • Collections in Java
  • Java ArrayList
  • Java LinkedList
  • Java Vector
  • Java HashSet
  • Java LinkedHashSet
  • Java TreeSet
  • Java HashMap
  • Java TreeMap
  • Java LinkedHashMap
  • Java Queue
  • Java PriorityQueue
  • Java Deque
  • Comparable interface
  • Comparator interface
  • Collections Interview Questions

MORE ...

  • Java Scanner Class
  • Java 8 Features
  • Java 9 Features
  • Java Conversion
  • Java Date
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations
  • Java main method
  • Java Interview Q

Copyright © 2012 – 2022 BeginnersBook . Privacy Policy . Sitemap