CharAt
This method returns a character in a string
at a certain index. The index is the argument. A valid index (one found in the string) must be used.
CharAt
returns a value, a character, that is similar to an int
. Meanwhile substring()
returns a new string
. We find it is faster to use charAt
when possible.
for
-loopOften we want to loop over the characters in a string
. In this example we use charAt
inside a for
-loop. We display each character to the console.
string
, the valid indexes are 0, 1 and 2. The last index is equal to the length minus 1.public class Program { public static void main(String[] args) { String value = "cat"; // Loop through all characters in the string. // ... Use charAt to get the values. for (int i = 0; i < value.length(); i++) { char c = value.charAt(i); System.out.println(c); } } }c a t
We use the charAt
method to get first and last characters in a string
(and also ones at certain offsets). Here we get the first two and last two chars.
public class Program { public static void main(String[] args) { String value = "intercept"; // First two characters. System.out.println(value.charAt(0)); System.out.println(value.charAt(1)); // Last two characters. System.out.println(value.charAt(value.length() - 1)); System.out.println(value.charAt(value.length() - 2)); } }i n t p
We must be careful when using charAt
. If we pass a negative index or an index that is past the last character in the string, we get an exception.
public class Program { public static void main(String[] args) { String value = "abc"; // Access a character out-of-range. char error = value.charAt(100); System.out.println(error); // Not reached. } }Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 100 at java.lang.String.charAt(Unknown Source) at Program.main(Program.java:6)
Benchmark, charAt
versus substring. Sometimes we can choose between charAt
and substring calls. In my testing, I found charAt
is faster to call.
charAt
to test a character in a string
. It also uses an if
-statement.string
, and test it in an if
-statement.charAt
to test a character is many times faster than taking a 1-character substring.public class Program { public static void main(String[] args) { // The example string. String value = "cat"; // Test results of our methods. System.out.println(value.charAt(2)); System.out.println(value.substring(2, 3)); long t1 = System.currentTimeMillis(); // Version 1: use charAt to test characters in a string. int count = 0; for (int i = 0; i < 10000000; i++) { if (value.charAt(2) == 't') { count++; } } long t2 = System.currentTimeMillis(); // Version 2: use substring to test characters in a string. count = 0; for (int i = 0; i < 10000000; i++) { if (value.substring(2, 3) == "t") { count++; } } long t3 = System.currentTimeMillis(); // ... Times. System.out.println(count); System.out.println(t2 - t1); System.out.println(t3 - t2); } }t t 0 5 ms: charAt 114 ms: substring
Often we need to access in strings. With charAt
, we have a simple and fast way to do this. We must check that an index is within range for the string
.