5.B] What are the different character extraction methods? Explain with a program.
Answer:
Java provides several methods to extract characters or sequences of characters from a String
object. These methods are useful for retrieving specific characters, substrings, or sequences from strings.
1. charAt(int index)
- Extracts a single character at a specified index.
- Syntax:
char ch = str.charAt(index);
- Example:
String str = "Hello"; char ch = str.charAt(1); System.out.println("Character at index 1: " + ch); // Output: e
2. getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)
- Extracts a sequence of characters from a string and stores them in a character array.
- Syntax:
str.getChars(srcBegin, srcEnd, dst, dstBegin);
- Example:
String str = "Hello"; char[] dst = new char[3]; str.getChars(1, 4, dst, 0); System.out.println("Extracted characters: " + new String(dst)); // Output: ell
3. toCharArray()
- Converts the entire string into a character array.
- Syntax:
char[] charArray = str.toCharArray();
- Example:
String str = "Hello"; char[] charArray = str.toCharArray(); System.out.println("Character array: " + Arrays.toString(charArray)); // Output: [H, e, l, l, o]
4. substring(int beginIndex)
- Extracts a substring from the specified begin index to the end of the string.
- Syntax:
String subStr = str.substring(beginIndex);
- Example:
String str = "Hello"; String subStr = str.substring(2); System.out.println("Substring from index 2: " + subStr); // Output: llo
5. substring(int beginIndex, int endIndex)
- Extracts a substring from the specified begin index to the specified end index.
- Syntax:
String subStr = str.substring(beginIndex, endIndex);
- Example:
String str = "Hello"; String subStr = str.substring(1, 4); System.out.println("Substring from index 1 to 4: " + subStr); // Output: ell
Program Example:-
Here’s a simple program that demonstrates the use of different character extraction methods:
public class CharacterExtractionExample { public static void main(String[] args) { String str = "Hello"; // 1. Using charAt() char ch = str.charAt(1); System.out.println("Character at index 1: " + ch); // Output: e // 2. Using getChars() char[] dst = new char[3]; str.getChars(1, 4, dst, 0); System.out.println("Extracted characters using getChars: " + new String(dst)); // Output: ell // 3. Using toCharArray() char[] charArray = str.toCharArray(); System.out.println("Character array using toCharArray: " + Arrays.toString(charArray)); // Output: [H, e, l, l, o] // 4. Using substring(beginIndex) String subStr1 = str.substring(2); System.out.println("Substring from index 2: " + subStr1); // Output: llo // 5. Using substring(beginIndex, endIndex) String subStr2 = str.substring(1, 4); System.out.println("Substring from index 1 to 4: " + subStr2); // Output: ell } }
Summary:-
charAt(int index)
: Extracts a single character at the specified index.getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)
: Extracts a sequence of characters into a character array.toCharArray()
: Converts the entire string into a character array.substring(int beginIndex)
: Extracts a substring from the specified index to the end of the string.substring(int beginIndex, int endIndex)
: Extracts a substring from the specified begin index to the specified end index.