Home » Java programming language

Java String getChars() Method with Example

Java String getChars() Method: Here, we are going to learn about the getChars() method with example in Java.
Submitted by IncludeHelp, on February 15, 2019

String getChars() Method

getChars() is a String method in Java and it is used to get the number of characters from the given index from the string.

It extracts the part of the string and stores to the character array. If an index is of out of range – it returns an exception.

Syntax:

    void source_string.getChars(
        srcStartIndex, 
        srcEndIndex, 
        target_charArray, 
        targetStartIndex);

Here,

  • source_string is a string from where we have to get the characters.
  • srcStartIndex is the starting position in the source_string.
  • srcEndIndex is the ending position in the source_string.
  • target_charArray is the output/targeted array in which we have store the copied characters.
  • targetStartIndex is the start position in the target_charArray.

Example:

    Input: 
    str = "Welcome at IncludeHelp"

    Function call:
    input.getChars(11, 22, output_str, 0);

    Output:
    output_str = "IncludeHelp"

Java code to get the characters from a string using String.getChars() method

public class Main
{
    public static void main(String[] args) {
        String input = new String("Welcome at IncludeHelp");
        char[] output = new char[11];
        
        //here, we are going to get IncludeHelp from the String
        //IncludeHelp is written from 11th 
        input.getChars(11, 22, output, 0);
        
        System.out.println("input  = " + input);
        System.out.println("output = " + String.valueOf(output));
    }
}

Output

input  = Welcome at IncludeHelp
output = IncludeHelp





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.
Learn PCB Designing: PCB DESIGNING TUTORIAL







Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.