Character Extraction
The Java String class provides methods for accessing or creating a substring from a longer string, i.e. character extraction. Like arrays, String indexes are zero based.
| Method | Purpose |
|---|---|
| charAt(int) | Returns the character at the specified location. |
| getBytes() | Encodes this String into a sequence of bytes using the platform’s default charset, storing the result into a new byte array. |
| getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin) | Copies the specified number of characters from the specified location into the char array. |
| substring(int) | Returns a substring beginning at the specified offset of the current string. |
| substring(int, int) | Returns a substring between the specified offsets of the current string. |
| char[ ] toCharArray( ) | Converts all the characters in a String object into a character array. It returns an array of characters for the entire string |
Extracting character at specified location from String
String indexes are zero based.
String x = "iByteCode";
char c = x.charAt(2);
System.out.println("charAt(2) in iByteCode is " + c);
Which prints,
charAt(2) in iByteCode is y
Bytes from String
We can get byte array from String.
| Method Signature |
|---|
| public byte[] getBytes() |
| public byte[] getBytes(Charset charset) |
| public byte[] getBytes(String charsetName) |
public class CharacterExtraction {
public static void main(String[] args) {
String x = "iByteCode";
byte bytes[] = null;
String s;
try {
bytes = x.getBytes("ASCII");
s = new String(bytes);
System.out.println(s);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
bytes = x.getBytes();
s = new String(bytes);
System.out.println(s);
}
}
iByteCode
iByteCode
Getting characters from String
| Method Signature |
|---|
| public void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin) where, srcBegin – starting index is zero-based srcEnd – end index (end index char will not be not be included) dst – the destination array. dstBegin – the start offset in the destination array. |
String str = new String("theopentutorials.com");
char charArray[] = new char[9];
str.getChars(7, 16, charArray, 0);
System.out.println(charArray);
Which prints,
tutorials
Extracting substrings
| Method Signature |
|---|
| public String substring(int beginIndex)throws IndexOutOfBoundsException |
| public String substring(int beginIndex, int endIndex)throws IndexOutOfBoundsException where, beginIndex is zero-based and endIndex is not |
public class CharacterExtraction {
public static void main(String[] args) {
String site = "iByteCode Training Solutions";
System.out.println(site.substring(10));//zero-based index
//1st argument zero-based and 2nd is not
System.out.println(site.substring(0,9));
}
}
Training Solutions
iByteCode
String to char array
We can get char array from String using toCharArray() method.
package com.ibytecode.strings.methods;
public class CharacterExtraction {
public static void main(String[] args) {
String str = new String("theopentutorials.com");
char[] ch = str.toCharArray();
for(char c1: ch)
System.out.print(c1);
}
}
Which prints,
theopentutorials.com
