FAQ: Working with Text - Get Parts of Strings

A better example would be to create 3 variables to get a first initial, second name initial and last name.

First initial with

char firstLetter = name[0];

Second initial with

int charPosition = name.IndexOf(" ");
charPosition = charPosition++;
char secondLetter = name[charPosition]

and then the last name, same as the existing example.

int stringPosition = name.IndexOf(“Abbasi”);

string lastName = name.Substring(stringPosition);

It’s working if you use string interpolation from the previous lesson.

just going to throw on the pile I had to come running to the discussion for this lesson to find out /what exactly do they want?!/
the lesson feels like it needs a rework.

thank god for this reply!! I am learning to code because I love how everything just makes sense, and logical! But on this exercise, I also thought “why would I define a variable for the actual first letter, if I am supposed to find it …” lol anyway. I hope they will change the exercise because it is indeed very confusing! :slight_smile:

Much better like this ^^

  *// User Name*
      string name = "Farhad Hesam Abbasi";

      *// Get first letter*
char firstLetter = name[0];

      *// Get last name*
int charPosition = name.IndexOf("Abbasi");
string lastName = name.Substring(charPosition);

      *// Print results*
Console.WriteLine("Hi Mr " + firstLetter + ". " + lastName);

I’ve been a .NET dev for 15 years and am just tinkering around here and going through the lessons for various reasons. I’m really puzzled that two years after the original post here, this question seems to be unimproved.

First, the instructions don’t follow the convention used in other lessons up until this point where they tell you step by step what to do. Here, they give you an implicit step (i.e. declaring the int to store charPosition and then using that in the bracket notation). You are left to clumsily hack away at the answer until most likely giving in and clicking the Hint which still is just the example text from the intro. The lesson steps would be clearer if they were broken out like in other lessons. I could have achieved the result multiple ways but since you have to submit the exact expected code, more explicit instructions would be less kludgy.

But even better than that would be to use more of a real world scenario where you are given a string to parse using conventional patterns. For example, it’s common enough in the real world to get a string containing a full name and having to parse out the individual names in the string. One way this might be achieved when there is no middle name is to get the first name by assuming it starts at the 0 index and use .IndexOf(" ") ( that’s the first space in the string) and pass it as the Length argument of .Substring.


string firstName = name.Substring(0, name.IndexOf(" "));

And the last name could be parsed using simply:

name.Substring(name.IndexOf(" ") + 1);

…in which the + 1 is there to exclude the space from the parsed result.

There are even better ways to parse out a name, especially when you can’t guarantee that it will only be a two part name. But these lessons haven’t yet introduced the learner to other methods that would be required like String.Split().

1 Like

agreed, this is a poor example. It says to get the first letter of the name string. Naturally we want to do this dynamically by pulling the letter at index 0

I completely agree with you. How can I know each name and surname? It should be dynamically.

I thought the same thing.

Am I wrong to think that its redundant to use .IndexOf() without a return to tell us what its indexing? Shouldn’t we print the result to console so that we’re being returned the information needed to complete the steps of this exercise?

By my experience just following the exercise will get you to memorize the use of indexof(), but you’re going to have to manually do the work of counting out the characters for the rest of it.

why i cant use this code
string firstLetter = name.Substring(0,1);
We have to anyway grab first letter of name.

It’s been 2yrs+ since this suggestion was made, so I doubt much will change. Weighing in regardless, the quality of this C# course is far below the Python courses I’ve also started, this is a prime example (there’s also one earlier on when it expects you to know lists in the first course, but doesn’t tell you that it’s not possible without previous C# knowledge.
I also spent quite a while trying to figure out why .IndexOf[0] wasn’t be accepted as an answer. Because in a real world application I’d expect there to be hundreds of names that need to automatically pull their first name. If I have to manually look through it and decide “F” is the first letter in the first name, I may as well just type the letter out myself no?
This course feels incredibly aged.

This lesson really seems like it was poorly thought out.

While they havn’t taught index number yet ( I think) This was a very Frustrating thing to go over.

If I know the name why am I looking for it in the code, I was looking for the last name by counting the spaces (Which will break if the user doesn’t have a last name)

Here we are in Feb 2022 and Codecademy still hasn’t addressed the muddy instructions in half of these lessons. I’m not gonna lie, even getting the Pro sub 50% off is still leaving me a little burnt when half of the instructions require me to come to the forum section and read through everyone else’s discussions to figure out where things went wrong in the lesson.

For this lesson specifically, I’m actually pretty annoyed. If you follow the instructions as they’re written, you will not pass the first step.

" First, use .IndexOf() and bracket notation to grab the first letter of the first name and save it to the variable firstLetter ."

Using int results in an error saying to use char. Using char results in an error telling you to use bracket notation. Well, you can’t perform the index.of action with bracket notation, that makes no sense. The error is skipping ahead and assuming you’ve already completed the first step of grabbing the index but the instructions make no indication you should be writing two lines of code here. It implies you should be using the firstLetter variable to grab the index, not for the char bracket notation step.

Cmon, Codecademy, you guys can do better than this. This course is riddled with BS instructions like this.

FWIW, if anyone is still stuck and wants to see a solution, here’s what I put to pass successfully (the console.writelines were just for fun at the end):

I was thinking the same. I am also a newbie C# student and i have to say, that this is not the most helpful exercise to do.

This lesson is the point at which I am questioning my Pro subscription. How is it useful to teach such a pointless exercise? I didn’t learn anything about how these things might actually be applicable.

Codeacademy is so highly rated, and the premise is really amazing, but these exercises are terrible and irrelevant to real-life application that I’m beginning to questions whether I’m even learning good practices by following this course.

1 Like

total lack of explanation! even when i compared my code to the solution i felt like i am guessing the entire time, only after i managed to somehow get the job done did i manage to understand, hope my code will surve as a better explanation to somebody:

using System;

namespace GetPartsofStrings
  class Program
    static void Main(string[] args)

      string name = "Farhad Hesam Abbasi";

      char firstLetter = name[0];    // first char is counted as 0 (could ba a space as well)

      int charPosition = name.IndexOf("H");              // locate H
      string lastName = name.Substring(charPosition);    // H and onwards
      Console.WriteLine($"{firstLetter}. {lastName}");   // intended method to print

      Console.WriteLine(firstLetter + ". " + lastName); // also works

      string test = "\n no errors";   // printed only when there are no errors (not related to the tasks)


I think I’ve experienced a bug that stops me from progressing - on the right side, I have the exercise with the string name “Farhad Hesam Abbasi”, but on the left side where the instructions are I’m supposed work with DNA “tga” string? Did 2 lessons get mixed up by any chance?

Same here, as the wrong instructions seems to be leading to complete this, I copied the code from ‘View Solution’ and emptied to work with.

using System;

namespace DNA


  class Program


    static void Main(string[] args)


      // dna strand

      string startStrand = "ATGCGATGAGCTTAC";

      // find location of "tga"

      // start point and stop point variables

      // define final strand

      // DNA mutation search