There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply () below.
If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.
Join the Discussion. Help a fellow learner on their journey.
Ask or answer a question about this exercise by clicking reply () below!
You can also find further discussion and get answers to your questions over in Language Help.
Agree with a comment or answer? Like () to up-vote the contribution!
1. The designer of C# is "Anders Hejlsberg" . His first name is nice, but we want to print the second name alone.
First, find the index of the space ( " " ) in the string designer and store it in a variable indexOfSpace .
2. Use Substring() and the variable indexOfSpace to get the second name. Store the returned value in a variable secondName .
The only thing I can recommend as a good practice is this:
Once you have declared the string indexOfSpace, the following would be much better when you don’t actually know how long the second string you want is:
The full length of the variable designer is 16.
In a case where we again don’t know how long the other string is, subtracting the index of the space (which is 6) from the full length: 16 - 6 = 10. This is useful for the program to know how far to go from the start (which is indexOfSpace)
Then, the .substring() method takes the second string starting from indexOfSpace, and ending at 10, and finally returns the wanted value. Using the above method works dynamically for any two strings separated by a space.
That was incredibly useful, thank you.
I am not convinced that this course explains the syntax very well.
Also the course progresses even if the code is wrong.
Struggled with this exercise (3. Capture Output 3/10).
Point 1 completed with a tick, but gave errors on the output window.
I could not get past point 2.
When I viewed the solution, i had missed the int from the variable.
I didn’t find the hints very helpful.
I currently have low confidence in learning C#.
I’ll file a bug report on the content because the output is a little misleading to learners. This is an observation and not a knock on the lesson writers. Notice that when you add the lines of code, you’ll see that a blank space precedes the last name Hejlsberg and the word by in the designBy string.
Another point: The String.Substring() has an optional parameter to give the length of the string you want output. If this length number takes the string past its end, you’ll see an error message.
If you know Python, see if you can reintrepret the code into Python – at least by using string_name.index(" ") or string_name.index("by").
Hi guys, I noticed the “correct” solution is with a trailing space before the last name. string secondName = designer.Substring(indexOfSpace); Console.WriteLine("–" + secondName + “–”);
Outputs: – Hejlsberg– (notice the trailing space)
I.M.O. it should be :
string secondName = designer.Substring((indexOfSpace + 1));
why we use designer.IndexOf(" ") not designer.IndexOf(“Hejlsberg”). Because both give same answer, which one is right. In previous lessons of .Indexof it states to use character or string we need as output between quotations. But how can it store correct string in the variable only by using space between ( " " ). What if there are paragraphs instead of two words. Can someone explain! Thanks
.IndexOf stores the first occurrence of a character in a string. Since in the string “Anders Hejlsberg” there is only one space, that’s the one that is stored.
In case you would need to find all spaces (or all occurrences of any character) in a larger string, you would probably have to use another method and/or “for” loops.
You can see some examples here or here.
Thanks to @harrjt for pointing out the empty space which gets printed in case only indexOfSpace is taken as a parameter for the Substring method. I added the +1 so that the substring starts from the next character, which is H.
As much as this makes sense, in this case Substring() doesn’t need the other output as it takes the first value as a starting point and returns all chars until the end of the string.
This:
designer.Substring(indexOfSpace);
//returns the exact same output as
designer.Substring(indexOfSpace, designer.Length - indexOfSpace);
//even if we had two surnames like "Hejsberg Somextrasurname" , it would return both
//so the second argument you recommend is redundant in this case
Hello
I’ll try to be as blatant as possible… In real life when you start to make apps, you will know where exactly to expect what type of input. So at some fields you know a user will enter his name and surname for example and in the other one his phone number. So you know by default to expect at least one spacing (the one between name and surname). You will not know the names of your users and ur program is not supposed to work or anticipate everyone’s name and surname like that… but what it can anticipate is a string that contains spacings. And like this you will always separate his name from the last name and you can then store them separately. So, for example, your app will then greet them like “Hello Name!” and not “Hello Name MiddleName MaidenName LastName”.