Home » Java programming language

Java StringBuilder getChars() method with example

StringBuilder Class getChars() method: Here, we are going to learn about the getChars() method of StringBuilder Class with its syntax and example.
Submitted by Preeti Jain, on December 22, 2019

StringBuilder Class getChars() method

  • getChars() method is available in java.lang package.
  • getChars() method is used to copy all the characters from the given arguments (int src_st, int src_end) into another destination array of char type like char[] dest.
  • In this method copy first character starts at index src_st and copying the last character ends at index src_end so all the copied characters will be placed in an array char[] dest and this array index starts at dest_st and ends at dest_beg+(src_end-src_beg)-1.
  • getChars() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
  • This method may throw an exception at the time of copying and placing copied characters.
    • IndexOutOfBoundsException – This exception may throw when src_st < 0 or dest_st < 0 or src_st > src_end or src_end > length().
    • NullPointerException – This exception may throw when char[] array is null exists.

Syntax:

    public void getChars(int src_st, int src_end, char[] dest, int dest_st);

Parameter(s):

  • int src_st – represents the index to start copying.
  • int src_end – represents the index to end copying.
  • int char[] dest – represents the array for copied elements.
  • int dest_beg – represents the index of starting position of char[] dest.

Return value:

The return type of this method is void, it returns nothing.

Example:

// Java program to demonstrate the example 
// of getChars(int src_st, int src_end, char[] dest, int dest_st)
// method of StringBuilder 

public class GetChars {
    public static void main(String[] args) {
        int src_st = 0;
        int src_end = 4;

        int dest_st = 0;

        // Creating an StringBuilder object
        StringBuilder st_b = new StringBuilder("Java World");

        // Display st_b
        System.out.println("st_b = " + st_b);

        char[] dest = new char[] {
            'a',
            'b',
            'c',
            'd',
            'e',
            'f',
            'g',
            'h',
            'i',
            'j'
        };

        // By using getChars() method is to copy the chars from the
        // given src_st to src_end of st_b and placed into dest[] 
        // and position of placing the copied chars starts at dest_st
        st_b.getChars(src_st, src_end, dest, dest_st);

        // Display destination array
        for (char val: dest)
            System.out.print("" + val);
    }
}

Output

st_b = Java World
Javaefghij





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





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.