We can also use built-in .NET methods to grab parts of strings or specific characters in a string.


.Substring() grabs part of a string using the specified character position, continues until the end of the string, and returns a new string. .IndexOf() is usually used first to get the specific character position.

string plantName = "Cactaceae, Cactus"; int charPosition = plantName.IndexOf("Cactus"); // returns 11 string commonName = plantName.Substring(charPosition); // returns Cactus

.Substring() is useful if we only want to use part of a string but keep the original data intact. So in this instance, we want to keep the string plantName, but just grab the "Cactus" portion of the string. We use .IndexOf() to find where "Cactus" starts, then use .Substring() with the position information to save "Cactus" to the new variable commonName.

We can also pass .Substring() a second argument, which will determine the number of characters in the resulting substring. For example, the following code shows how we can use .Substring() with two arguments to specify the length of our substring:

string name = "Codecademy"; int start = 2; int length = 6; string substringName = name.Substring(start, length); // returns 'decade'

Bracket Notation

Bracket notation is a style of syntax that uses brackets [] and an integer value to identify a particular value in a collection. In this case, we can use it to find a specific character in a string.

string plantName = "Cactaceae, Cactus"; int charPosition = plantName.IndexOf("u"); // returns 15 char u = plantName[charPosition]; // returns u

Similar to the example above, we first use .IndexOf() to grab the character position, which in this case is 15. We then take the string value and append it with a set of brackets [] and place the charPosition value inside the brackets.



In program.cs, you are given a string defined as startStrand. Your goal is to find the ending condition for a DNA strand, "TGA", and then print out the substring of startStrand that starts from the first character and goes through "TGA". This will output a valid DNA strand.

You don’t need much understanding of biology to follow these directions, but if you’d like a more in-depth explanation/background, click the dropdown below:

More info on DNA

DNA is composed of a series of nucleotides abbreviated as:

  • A: Adenine
  • C: Cytosine
  • G: Guanine
  • T: Thymine


So a strand of DNA could look something like ACGAATTCCG.

A protein has the following qualities (along with a few others):

  1. It begins with a “start codon”: ATG.
  2. It ends with a “stop codon”: TGA.

First, use IndexOf() to find the location of "TGA" in startStrand. Save this as a variable called tga.


Create two variables called startPoint and length. startPoint should be set to 0, and length should be set to three more than the value of tga.

length should be set to three more than the value of tga for two reasons:

  • IndexOf() returns the starting point of tga in startStrand (where the character "T" is). To capture "GA" as well, add two.
  • When using .Substring() with two arguments (which you will do next), it captures the number of characters indicated by the second argument. Because strings in C# are zero-indexed, you will need to add one more in order to capture the last character. In this example, the "A" in "TGA" falls at index 8 but is the 9th character — if we only set the length to 8, the final “A” would not be included. Therefore, instead of defining length as tga + 2, you should define it as tga + 3.

Now use Substring() to retrieve the substring of startStrand that starts from startPoint and has a length of length. Save this to a variable called dna.

Print out this string using Console.WriteLine(dna).


You are worried there might have been a mutation with this strand of DNA! Use bracket notation to grab the seventh character in dna.

  • If it is equal to G, there has been no mutation.
  • If it is equal to T or A, there has been a mutation.

Show your result in the console. Has there been a mutation?

Take this course for free

Mini Info Outline Icon
By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.

Or sign up using:

Already have an account?