Loading...

C# String Methods

plant.Length counts the letters plant already holds. string plant = "leaf"; still stores leaf, and Console.WriteLine(plant.Length) writes 4. The letters stay leaf. They do not turn into the number. The dot after the name is the question, and the writing line shows the answer. Keep the public class named Pine. You are not installing the SDK again.

The dot asks how many letters

Open the Pine file. The class name stays Pine. This page does not rename the type. The last page kept string plant = "leaf"; and one writing line that showed leaf. This page keeps that storing line. Delete an int line if one is still there. Delete any ReadLine line. The file you keep has no wait and no whole-number line. The count is written only by the writing line that asks the question.

Inside Main, the storing line is still string plant = "leaf";. string is the kind word. plant is the name. The quotes hold l, e, a, f, all small. That line stores the letters. It does not ask a question, and it does not write the screen. The question is more work after the letters are stored. After the name, write a dot, then the word Length. There are no parentheses after Length in C#. The whole question is plant.Length. The dot ties the question to the name. Length is the word for how many. Asking is not the same as storing. The letters in the name stay leaf. The question does not take the quotes away, and it does not put the number 4 into the name plant.

The file you keep writes the letters first, then asks the question. The first writing line is the bare name: Console.WriteLine(plant);. No quotes, and no dot. It writes leaf and ends the line. The second writing line is the question: Console.WriteLine(plant.Length);. No quotes around Length. C# reads the name plant, counts the letters that name is holding, writes that count, and ends the line. For the file you keep, the count is 4. The quote marks are not on the screen. The word Length is not on the screen. The letters of the name are not on the screen. Save the file.

The file you keep

Keep this teaching shape. One storing line, string plant = "leaf";. One writing line for the letters. One writing line for the count. The count line is plant.Length inside WriteLine. Nothing else.

the file you keep

csharp
using System;

public class Pine
{
    public static void Main()
    {
        string plant = "leaf";
        Console.WriteLine(plant);
        Console.WriteLine(plant.Length);
    }
}

Read it from the top. using System; prepares short names. public class Pine names the type. public static void Main() is where the runtime starts. The storing line is string plant = "leaf";. It stores four letters. It writes nothing. Console.WriteLine(plant); writes leaf and ends the line. Console.WriteLine(plant.Length); writes 4 and ends the line. Two lines on the screen. The first line is the letters. The second line is the count. The quotes stay in the file, around leaf, and they are on neither screen line.

Run it with dotnet run. The program writes leaf, then 4 on the next line. It does not wait for you to type. The letters were already in the file, and the count is answered from those letters. If dotnet run waits, ReadLine is still in the file. Delete that line and keep the storing line and the two writing lines. The screen shows leaf, then 4. If the screen shows only leaf, the second writing line is missing. If the screen shows only 4, the first writing line is missing. If the screen shows 4 and then leaf, the two writing lines are swapped. You do not reinstall the .NET SDK for a screen that shows the wrong line.

Length and a place

Keep the two questions easy to tell apart.

📊 String question habits for plant

Question Needs Screen for leaf
plant.Length no parentheses 4
plant[0] place 0 inside brackets l
plant[1] place 1 inside brackets e
plant.Length() parentheses after Length build writes an error

A space counts as one

The letters between the quotes can change, and the count follows those letters. Under the first question, store a new set of letters in the same name, then ask the question again. The new storing line is plant = "leaf stem";. It does not start with string. Between the quotes there are nine places: l, e, a, f, one space, s, t, e, m. The space is one of them. Length counts it. This second file does not write the letters. It writes only the two counts, so the screen cannot be confused with the letters themselves.

This is the second file. It is not the file you keep. The file you keep has one storing line, the letters leaf, no space, and a screen of leaf then 4. Use this second shape when you want to see the space counted, then put the kept file back.

a space counted by Length

csharp
using System;

public class Pine
{
    public static void Main()
    {
        string plant = "leaf";
        Console.WriteLine(plant.Length);
        plant = "leaf stem";
        Console.WriteLine(plant.Length);
    }
}

Run dotnet run. The screen shows 4, and the next line is 9. Two lines. No quote marks. The letters leaf are not on the screen. Only the counts are there. If the second line is 8, the space is missing between the quotes, and the stored letters are leafstem. If both lines are 4, the second storing line is missing or still says "leaf". Do not write string a second time for the same name. The name is created once. Later lines only change the letters it holds, or ask a question about those letters.

Which letter sits at a place

Length asks how many. A different question asks which letter sits at one place. The place goes inside square brackets after the name. The first place is 0, not 1. For leaf, place 0 holds l, place 1 holds e, place 2 holds a, and place 3 holds f. plant[0] answers with the single letter l. plant[1] answers with the single letter e. The answer is one letter, not the whole word leaf, and not the place number itself. The letters stored in plant stay leaf.

This is the third file. It is not the file you keep. The file you keep asks Length, and its screen is leaf then 4. Use this third shape when you want to see the places, then put the kept file back.

the first two places

csharp
using System;

public class Pine
{
    public static void Main()
    {
        string plant = "leaf";
        Console.WriteLine(plant[0]);
        Console.WriteLine(plant[1]);
    }
}

Run dotnet run. The screen shows l, and the next line is e. Two lines. No quote marks. The word leaf is not on the screen. The numbers 0 and 1 are not on the screen. If the first line is e, the first question asked for place 1, which is the second letter. The first letter is place 0. Place 4 is not one of the letters in leaf. The places that exist are 0, 1, 2, and 3. A writing line with plant[4] can pass the build and then fail when the program runs. Put plant[0] and plant[1] back if you still want those two lines, then put the kept file back when you are done.

Length does not take parentheses

In C#, Length is not written with an empty pair of parentheses. The line Console.WriteLine(plant.Length()); puts () after Length. The build writes an error. It does not stay quiet. There is no screen to trust while the build is writing an error. The repair is to delete the parentheses, so the line is Console.WriteLine(plant.Length);. Keep leftover Java length() habits mapped to the C# word Length with no parentheses on this page.

This fourth file is the parentheses mistake. It is not the file you keep. Keep it only until the build writes the error, then put plant.Length back with no parentheses.

Length written with parentheses

csharp
using System;

public class Pine
{
    public static void Main()
    {
        string plant = "leaf";
        Console.WriteLine(plant.Length());
    }
}

The screen shows the answer, and the letters stay

You can now ask how many letters a name already holds, and which letter sits at a place that starts at 0. string plant = "leaf"; still stores leaf. Console.WriteLine(plant); writes leaf. Console.WriteLine(plant.Length); writes 4, and the letters stay leaf. A later plant = "leaf stem"; makes the next count 9, because the space counts as one. plant[0] writes l, and plant[1] writes e. Length() with parentheses makes the build write an error. The next page leaves these questions. It stays with a whole number like the 4 this count wrote, and the number is one you write in the file again.

🧠 Test Your Knowledge

Ready to Start

Test Your Knowledge

Challenge yourself with this interactive quiz and see how well you understand the topic

❓
6
Questions
🎯
70%
To Pass
♾️
∞
Time
🔄
∞
Attempts

📝 Instructions

  • Read each question carefully
  • Select the best answer for each question
  • You can retake the quiz as many times as you want
  • Your progress will be shown at the top