getChars()
If you need to extract more than one character at a time, you can use the getChars( ) method.
It has this general form:
void getChars(int sourceStart, int sourceEnd, char target[ ], int targetStart)
- sourceStart specifies the index of the beginning of the substring
- sourceEnd specifies an index that is one past the end of the desired substring.
The substring contains the characters from sourceStart through sourceEnd-1. The array that will receive the characters is specified by target. The index within target at which the substring will be copied is passed in targetStart.
To extract more than one character, use the getChars( ) method.
The following program demonstrates getChars( ). It gets a sub set of the chars.
public class Main {
public static void main(String args[]) {
String s = "This is a test string from java2s.com.";
int start = 10;
int end = 14;
char buf[] = new char[end - start];
s.getChars(start, end, buf, 0);
System.out.println(buf);
}
}
Here is the output of this program:
test
Home
Java Book
Essential Classes
Java Book
Essential Classes
String:
- String type and Literals
- String Concatenation
- String.CASE_INSENSITIVE_ORDER
- String Constructor
- charAt(int index):Get a single char by index
- String: compareTo(String stringValue)
- concat(String str)
- equals():Compare two string value for equality
- equals( ) vs ==
- contains(CharSequence s)
- copyValueOf(char[] data)
- endsWith(String suffix)
- format():Format a string
- getBytes():Get byte array from string
- getChars()
- indexOf
- intern a string
- isEmpty:if string is empty
- lastIndexOf()
- length() Returns the length of this string
- startsWith( )
- toLowerCase() and toUpperCase(): convert string case with locale
- substring:Get sub string from a string
- toCharArray():Get char array from string
- toString( )
- trim()
- valueOf():Convert boolean, char, double, float,int,long,object to String