FAQ: Method Calls and Input - Capture Output

Just learnt that the whole string will be captured if I don’t add int count in Substring function, thanks for code demo

using System;

namespace CaptureOutput
{
  class Program
  {
    static void Main(string[] args)
    {
      string designer = "Anders Hejlsberg";
      int lengthOfString = designer.Length;
      int indexOfSpace = designer.IndexOf(" ", 0 , lengthOfString);
      string secondName = designer.Substring(indexOfSpace + 1);
      Console.WriteLine(secondName);
    }
  }
}

That is my answer.

code compiler is malfunctioning but this is an opportunity to investigate and understand better way…

That is my answer like,

    {
        string designer = " Andres Hjinberg";
        int indexOfSpace = designer.IndexOf("H");
        string secondName = designer.Substring(indexOfSpace);
        Console.WriteLine(secondName);

    }

by the way my advice is to always check your code in C#.

1 Like

Anyone else have this error code in their console? I’m going through all the steps fine and completing the instructions, even getting the green ticks but everytime I run the code I get this error message and the actual code doesn’t run…

Program.cs(10,29): error CS0029: Cannot implicitly convert type ‘int’ to ‘string’ [/home/ccuser/workspace/csharp-capture-output/c-sharp-hello-world-run-some-c-sharp.csproj]

The build failed. Fix the build errors and run again.

I encountered the same error code. I suggest looking up the solutions to the right code from codeacademy.com and then double-checking your code to see if there are any mistakes.

I’ve noticed that codeacademy.com sometimes displays a green checkmark even when a code is not entirely correct. So even if the logic of your code is correct, it might still contain some incorrect script.

I get that a bunch of people here want to help but I think the point of these small projects is for us students to also notice how some of it works. For example, I noticed that on the terminal, it was printing as " Hejlsberg", with the extra space.

While the lesson doesn’t show you how to clean this up, I took this into my own hands as a beginner and just added, “indexOfSpace + 1” to the code, because I thought I could just move where the Substring starts by one point. Of course this works.

Don’t get me wrong, I’m reading the top comment here and now I’m wondering what the difference is between my solution and that one. But I guess that’s what is pretty amazing about learning to code. I’m sure somewhere down the road, I will actually get hands-on experience about why timmskiller’s code is better.

As for jcon, the only advice I can give you is to also use a separate terminal; I’ve installed VSCode on my machine for example, and I run my code through the codecademy terminal, as well as this other one. I’ve noticed that the terminal on the browser can sometimes be picky about the comments and debugging I perform (sometimes having to delete it in order to get my green checkmark) and it won’t actually tell me why this is wrong… spoiler, it’s not. But that’s exactly why I want to get a ‘second opinion’, if that makes sense (by running the code in VSCode).

Cheers all

What if I want to print the only the 3rd name:
or only the Second Name.
Anders del Hejlsberg ?
how will I do ? I tried but I failed, may be I am not understanding this method itself , coz using Loop I am able to Figure out/ able to Solve it but not using this.
but using this Substring Method I failed , I tried but it still prints the “Second followed by Third Name” ,
a)) I tried Using this Method to Print " only the Third Name" on my Console but Failed , Here are the Sample, I tired.

string designer = "Anders del Hejlsberg ";

  int indexOfSpace = designer.IndexOf(" ");
  
 
  string secondName = designer.Substring(indexOfSpace);
  int secondIndexOfSpace = secondName.IndexOf(" ");
  string thirdName = secondName.Substring(secondIndexOfSpace );
  Console.WriteLine(thirdName);