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 StringBuffer getChars()

By Chaitanya Singh | Filed Under: java

In this guide, we will discuss Java StringBuffer getChars() method with examples.

Syntax of getChars() method

//it will copy the chars from index 1 to 8
//and place it at 3rd index in chArray
sb.getChars(1, 8, chArray, 3)

Here, sb is an object of StringBuffer class.

getChars() Description

public void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin): This method copies the character sequence of StringBuffer instance from index srcBegin till index srcEnd. It places the copied sequence into destination array dst starting from index dstBegin.

To Summarize:
Characters copied from StringBuffer: From srcBegin till srcEnd -1 as srcEnd is exclusive.
Copied characters in destination char array starts from index dstBegin and end at (dstBegin + (srcEnd -srcBegin - 1))

getChars() Parameters

This method takes four parameters:

  • srcBegin: Sequence is copied starting from this index.
  • srcEnd: Sequence is copied till this index.
  • dst: Destination char array.
  • dstBegin: Index in dst array, copied sequence placed in dst at this index.

getChars() Return Value

  • It does not return anything.

It throws IndexOutOfBoundsException, if any of the following condition occurs:

  • If index srcBegin < 0.
  • If index dstBegin < 0.
  • If srcBegin > srcEnd.
  • If srcEnd > sb.length().
  • If dstBegin+srcEnd-srcBegin > dst.length().

Example 1

public class JavaExample {
  public static void main(String[] args) {
    StringBuffer sb = new StringBuffer("Hello World!");
    System.out.println("Char Sequence: "+sb);
    char[] chArray = new char[] {'h', 'i', 'f', 'r', 'i','e','n','d'};
    System.out.print("Given char Array: ");
    System.out.println(chArray);

    //copy sequence from index 6 to 12 into chArray
    sb.getChars(6,12,chArray, 2);
    System.out.print("char Array after copy: ");
    System.out.println(chArray);
  }
}   

Output:

Java StringBuffer getChars() Output 1

Example 2

public class JavaExample {
  public static void main(String[] args) {
    StringBuffer sb = new StringBuffer("Hello World!");
    System.out.println("Char Sequence: "+sb);
    char[] chArray = new char[] {'h', 'i'};
    System.out.print("Given char Array: ");
    System.out.println(chArray);

    //copy sequence from index 6 to 12 into chArray
    sb.getChars(6,12,chArray, 2);
    System.out.print("char Array after copy: ");
    System.out.println(chArray);
  }
}   

Output: The destination array size is only 2 so it cannot hold 8 more characters.

Java StringBuffer getChars() Output 2

Recommended Posts

  • Java StringBuffer indexOf()
  • Java StringBuffer lastIndexOf()
  • Java StringBuffer length()
  • Java StringBuffer trimToSize()
❮ Java StringBuffer

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