Working with Text : Get Parts of a String

https://www.codecademy.com/courses/learn-c-sharp/lessons/csharp-working-with-text/exercises/get-parts-of-strings?action=resume_content_item

I get this error:

Preformatted textProgram.cs(19,11): error CS1001: Identifier expected [/home/ccuser/workspace/csharp-working-with-text-get-parts-of-strings/e7-workspace.csproj]
Program.cs(19,11): error CS1002: ; expected [/home/ccuser/workspace/csharp-working-with-text-get-parts-of-strings/e7-workspace.csproj]
Program.cs(19,12): error CS1002: ; expected [/home/ccuser/workspace/csharp-working-with-text-get-parts-of-strings/e7-workspace.csproj]

The build failed. Please fix the build errors and run again.

This is my code:

using System;

namespace NameGrab
{
class Program
{
static void Main(string args)
{
// User Name
string name = “Farhad Hesam Abbasi”;

  // Get first letter

int charPosition = name.IndexOf(“F”); //returns 0
char firstLetter = name[charPosition];

  // Get last name
  	
  int 2ndcharPosition = name.IndexOf("Hesam Abbasi");
  //returns 6
  
  	string lastName = name.Substring(lastName);
  //returns Abbasi

  // Print results


}

}
}

It says I can click next, but I clearly made a mistake? Why am I allowed to go next?

Hi @jevinr

You’ve quoted your code, rather than using the </> code formatting option in the editor which isn’t great for us to be able to copy and run your code to help troubleshoot.

The actual root cause of your error:
Preformatted textProgram.cs(19,11): error CS1001: Identifier expected [/home/ccuser/workspace/csharp-working-with-text-get-parts-of-strings/e7-workspace.csproj]
is your variable declaration here:
int 2ndcharPosition = name.IndexOf("Hesam Abbasi");

It’s not valid syntax in C# to start a variable name with a number.

As for why it allowed you to progress? I can only assume that, despite the error in your code, you’ve met whatever the requirements were for the SCT. (I guess it must just be looking, maybe using regex, for you to have included the method calls the instructions ask for.)

Thank you so much! I am sorry, I will paste code with </>

1 Like

Don’t worry about it this time. The error was fairly obvious, so there was no real need to run your code and try to debug it.

Using the </> code option in future will make it easier for us if you get stuck with any other exercises. :grin: