Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Java Arrays
  • Java Strings
  • Java OOPs
  • Java Collection
  • Java 8 Tutorial
  • Java Multithreading
  • Java Exception Handling
  • Java Programs
  • Java Project
  • Java Collections Interview
  • Java Interview Questions
  • Java MCQs
  • Spring
  • Spring MVC
  • Spring Boot
  • Hibernate
Open In App
Next Article:
StringBuffer getChars() method in Java with Examples
Next article icon

Java String getChars() with examples

Last Updated : 04 Feb, 2022
Comments
Improve
Suggest changes
Like Article
Like
Report

The java string getChars() method copies characters from the given string into the destination character array.
Syntax: 
 

public void getChars(int srhStartIndex,  int srhEndIndex, char[] destArray, int destStartIndex)      Parameters: srhStartIndex : Index of the first character in the string to copy.  srhEndIndex : Index after the last character in the string to copy. destArray : Destination array where chars will get copied. destStartIndex : Index in the array starting from where the chars                  will be pushed into the array. Return: It does not return any value.

Exception: StringIndexOutOfBoundsException – If srhStartIndex, srhEndIndex are not in proper range.
Example : To show working of getChars() method 
 

java




// Java program to demonstrate
// working of getChars() method
 
class Gfg1 {
    public static void main(String args[])
    {
        String str = "Welcome! to GeeksforGeeks";
 
        char[] destArray = new char[20];
        try {
            str.getChars(12, 25, destArray, 0);
            System.out.println(destArray);
        }
        catch (Exception ex) {
            System.out.println(ex);
        }
    }
}
 
 

Output: 
 

GeeksforGeeks

 

java




// Java program to demonstrate
// exception condition in
// working of getChars() method
 
class Gfg2 {
    public static void main(String args[])
    {
        String str = "Welcome! to GeeksforGeeks";
 
        char[] destArray = new char[20];
        try {
            // Starting index 0 and ending index 24
            str.getChars(12, 26, destArray, 0);
            System.out.println(destArray);
        }
        catch (Exception ex) {
            System.out.println(ex);
        }
    }
}
 
 

Output: 
 

java.lang.StringIndexOutOfBoundsException: String index out of range: 26

 



Next Article
StringBuffer getChars() method in Java with Examples

N

Niraj_Pandey
Improve
Article Tags :
  • Java
  • Java-Functions
  • Java-lang package
  • Java-Strings
Practice Tags :
  • Java
  • Java-Strings

Similar Reads

  • StringBuilder getChars() in Java with Examples
    The getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin) method of StringBuilder class copies the characters starting at the given index:srcBegin to index:srcEnd-1 from String contained by StringBuilder into an array of char passed as parameter to function. The characters are copied from Str
    3 min read
  • StringBuffer getChars() method in Java with Examples
    The getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin) method of StringBuffer class copies the characters starting at the index:srcBegin to index:srcEnd-1 from actual sequence into an array of char passed as parameter to function. The characters are copied into the array of char dst[] star
    3 min read
  • Java String join() with examples
    The java.lang.string.join() method concatenates the given elements with the delimiter and returns the concatenated string.Note that if an element is null, then null is added.The join() method is included in java string since JDK 1.8. There are two types of join() methods in java string. : public sta
    2 min read
  • ByteBuffer getChar() method in Java with Examples
    getChar() The getChar() method of java.nio.ByteBuffer class is used to get method for reading a char value Reads the next two bytes at this buffer's current position, composing them into a char value according to the current byte order, and then increments the position by two. Syntax: public abstrac
    6 min read
  • URL getQuery() method in Java with Examples
    The getQuery() function is a part of URL class. The function getQuery() returns the Query of a specified URL. Function Signature: public String getQuery() Syntax: url.getQuery() Parameter: This function does not require any parameter Return Type: The function returns String Type Query of a specified
    2 min read
  • StringBuilder charAt() in Java with Examples
    The charAt(int index) method of StringBuilder Class is used to return the character at the specified index of String contained by StringBuilder Object. The index value should lie between 0 and length()-1. Syntax: public char charAt(int index) Parameters: This method accepts one int type parameter in
    3 min read
  • String replace() method in Java with Examples
    The String replace() method returns a new string after replacing all the old characters/CharSequence with a given character/CharSequence. Example:Return a new string where all " o" characters are replaced with "p" character: Java Code // Java program to demonstrate // the replace() method public cla
    4 min read
  • StringBuilder length() in Java with Examples
    The length() method of StringBuilder class returns the number of character the StringBuilder object contains. The length of the sequence of characters currently represented by this StringBuilder object is returned by this method. Syntax: public int length() Return Value: This method returns length o
    2 min read
  • javap tool in Java with Examples
    javap tool The javap tool is used to get the information of any class or interface. The javap command (also known as the Java Disassembler) disassembles one or more class files. Its output depends on the options used (“-c” or “-verbose” for byte code and byte code along with innards info, respective
    2 min read
  • Locale toString() Method in Java with Examples
    The toString() method of Locale class in Java is used to return a string representation of this locale. Each element i.e., the language, country or the variant of the locale is separated by underbars Syntax: LOCALE.toString() Parameters: This method does not take any parameters. Return Value: This m
    1 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences