Get string character by index
Categories:
How to Get a Character from a String by Index in Java

Learn the various methods to access individual characters within a Java String using their index, including charAt()
, toCharArray()
, and codePointAt()
.
In Java, a String
is an immutable sequence of characters. Often, you'll need to access a specific character at a given position (index) within a string. Java provides several straightforward methods to achieve this, each with its own use cases and advantages. This article will guide you through the most common and effective ways to retrieve a character by its index.
Understanding String Indexing in Java
Before diving into the methods, it's crucial to understand how string indexing works in Java. Like most programming languages, Java uses zero-based indexing. This means the first character of a string is at index 0, the second at index 1, and so on. The last character of a string of length N
will be at index N-1
.
flowchart LR A["String: 'Hello'"] A -- Index 0 --> B[H] A -- Index 1 --> C[e] A -- Index 2 --> D[l] A -- Index 3 --> E[l] A -- Index 4 --> F[o]
Visual representation of zero-based string indexing.
Method 1: Using charAt()
The charAt()
method is the most common and direct way to get a character at a specified index. It returns the char
value at the specified index. The index must be within the range of 0 to length() - 1
. If the index is out of this range, it throws an IndexOutOfBoundsException
.
String myString = "Java Programming";
char firstChar = myString.charAt(0); // 'J'
char fifthChar = myString.charAt(4); // ' '
char lastChar = myString.charAt(myString.length() - 1); // 'g'
System.out.println("First character: " + firstChar);
System.out.println("Fifth character: " + fifthChar);
System.out.println("Last character: " + lastChar);
Using charAt()
to retrieve characters by index.
charAt()
is valid (between 0 and string.length() - 1
) to avoid IndexOutOfBoundsException
. You can check the index using an if
condition or a try-catch
block.Method 2: Converting to a Character Array with toCharArray()
Another approach is to convert the entire String
into a char
array using the toCharArray()
method. Once you have a char[]
, you can access individual characters using array indexing, which is similar to string indexing.
String myString = "Hello World";
char[] charArray = myString.toCharArray();
char firstChar = charArray[0]; // 'H'
char seventhChar = charArray[6]; // 'W'
System.out.println("First character: " + firstChar);
System.out.println("Seventh character: " + seventhChar);
Accessing characters after converting a string to a char
array.
toCharArray()
provides array-like access, it creates a new char
array in memory. For accessing a single character, charAt()
is generally more efficient as it avoids this overhead. Use toCharArray()
when you need to iterate over or manipulate multiple characters in the string.Method 3: Handling Unicode with codePointAt()
Java's char
type is a 16-bit unsigned integer, representing Unicode characters in the Basic Multilingual Plane (BMP). However, some Unicode characters (supplementary characters) require two char
values (a surrogate pair). The charAt()
method returns a single char
, which might not represent a complete supplementary character.
For proper handling of all Unicode characters, especially supplementary ones, codePointAt()
is the preferred method. It returns the Unicode code point value at the specified index. If the character at the given index is part of a surrogate pair, codePointAt()
will return the full code point.
String unicodeString = "Hello \uD83D\uDE00 World"; // "Hello đ World"
// Using charAt() - might return only half of a surrogate pair
char char1 = unicodeString.charAt(6); // \uD83D (first part of đ)
char char2 = unicodeString.charAt(7); // \uDE00 (second part of đ)
// Using codePointAt() - returns the full code point
int codePoint = unicodeString.codePointAt(6); // 128512 (decimal for đ)
System.out.println("Char at index 6 (charAt): " + String.format("%x", (int)char1));
System.out.println("Char at index 7 (charAt): " + String.format("%x", (int)char2));
System.out.println("Code point at index 6 (codePointAt): " + String.format("%x", codePoint));
System.out.println("Character from code point: " + new String(Character.toChars(codePoint)));
Demonstrating codePointAt()
for handling Unicode characters.
codePointAt()
is essential for correctly handling characters that might span multiple char
units. Using charAt()
in such scenarios could lead to incorrect character representation or truncation.