Get Character at Specific Index in String

To get character at specific index in the given string in Dart, we can use square bracket notation and specify the index in the square brackets.

The syntax of the expression to get the character in the string myString at index i is


Dart Program

In the following program, we take a string 'abcdef' in variable myString, and get the character in this string at index=3.


void main() {
    var myString = 'abcdefg';
    var index = 3;
    if (index >= 0 && index < myString.length) {
      var output = myString[index];
      print('myString[$index] = $output');
    } else {
      print('index out of range for given string.');


myString[3] = d

In the program, before accessing the character at given index in the string, we have checked in the index is in range of the string, so that when accessing the character we do not get any error.



In this Dart Tutorial, we learned how to get the character at specified index in the given string in Dart.