Menu
Coddy logo textTech

Accessing Characters

Lesson 3 of 14 in Coddy's Strings and Arrays in C# course.

To access separate characters in a string, we can use index.

The index of the first character is 0, the second character is 1, and so on. 

To access it, use the [] operator,

string myStr = "Hello";
char result = myStr[0];

In the above example, result will hold the character H.

quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

challenge icon

Challenge

Easy

Write a function named blurb that gets a string as an argument. The function returns the concatenation of the characters in the 1st, 3rd and 5th indexes.

Because that [] operation returns a char, you need to cast it to a string in order to perform the concatenation, this is how it's done:

char c1 = 'a';
char c2 = 'b';
char c3 = 'c';

string concatenation = Char.ToString(c1) + Char.ToString(c2) + Char.ToString(c3);
Console.WriteLine(concatenation) // Outputs abc

Try it yourself

using System;

class Blurb {
    public static string blurb(string s) {
        // Write code here
    }
}

All lessons in Strings and Arrays in C#