function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Elad Kaplan 3Elad Kaplan 3 

How to get character at index from a string

I have a String and I'm trying to get it's characters by index.

For some reason the method string.charAt(index) , doesn't return a letter, but a number..

Any idea how can I get the characters ?
You can get at the string equivalent of the character for that index by using substring(index, index+1).  You can't really get at the characters as strings are stored as unicode which may be multi-byte, and Apex doesn't have a character type.
Hemanth SayanaHemanth Sayana
We can use substring(string.length()-1) to get last index value. To get specific index we need to modify the index using string.length()-2 or string.length()-3 and so on.
Tejas Pawar 29Tejas Pawar 29
@Elad there is one fine solution to get char from the string is to split it and then access the specified character from the output list.
Sahil Sharma 36Sahil Sharma 36

You may use the following code:
List<String> strList = str.split('');
str is the sample string ‘abcd’
str.split('') returns the list of characters that can be traversed {‘a’, ‘b’, ‘c’, ‘d’}