Java String getChars() with examples (original) (raw)
Last Updated : 04 Feb, 2022
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
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
class
Gfg2 {
`` public
static
void
main(String args[])
`` {
`` String str = "Welcome! to GeeksforGeeks";
`` char
[] destArray =
new
char
[
20
];
`` try
{
`` 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
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
- Field getChar() method in Java with Examples The getChar() method of java.lang.reflect.Field is used to get the value of char which has to be static or instance field type. This method also used to get the value of another primitive type convertible to type char via a widening conversion. When a class contains a static or instance Char field a 3 min read
- StringWriter getBuffer() method in Java with Examples The getBuffer() method of StringWriter Class in Java is used to get the StringBuffer representation of this StringWriter instance. This method does not accepts any parameter and returns the required StringBuffer value. Syntax: public StringBuffer getBuffer() Parameters: This method accepts does not 2 min read
- StringBuilder setCharAt() in Java with Examples The setCharAt(int index, char ch) method of StringBuilder class is used to set the character at the position index passed as ch. This method changes the old sequence to represents a new sequence which is identical to old sequence only difference is a new character ch is present at position index. Th 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
- Java String toCharArray() with example In Java, the toCharArray() method of the String class converts the given string into a character array. The returned array length is equal to the length of the string. This is helpful when we need to work with individual characters of a string for tasks like iteration or modification. Example: Below 2 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