<style> .reveal-up, .reveal-left, .reveal-right, .reveal-stagger > * { opacity: 1 !important; transform: none !important; } </style>
  1. Learn
  2. C#
  3. Getting started
All lessons in this course

Lesson 6 of 60 14 minBeginner

Strings, input and output

Read input from the user, build text with interpolation, and use the most important string methods.

Almost every program works with text: names, messages, file paths, user input. In C# text is a string, a sequence of characters.

Reading input

Greeting.cs
Console.Write("What is your name? ");string? name = Console.ReadLine();   // waits until Enter is pressedConsole.WriteLine("Hello, " + name + "!");
Output
What is your name? Ravi
Hello, Ravi!

Building strings

Program.cs
string first = "Ravi";int age = 25; string a = "Name: " + first + ", age " + age;   // concatenationstring b = $"Name: {first}, age {age}";          // interpolation (preferred)string c = $"Next year: {age + 1}";              // any expression in { }string d = $"Price: {1499.5:F2}";                // format: 2 decimals Console.WriteLine(b);Console.WriteLine(d);
Output
Name: Ravi, age 25
Price: 1499.50

Escape sequences and verbatim strings

WriteTo get
\na new line
\ta tab
\"a double quote inside a string
\\a backslash
Program.cs
Console.WriteLine("Line 1\nLine 2");Console.WriteLine("She said \"Hi\"");string path = @"C:\Projects\Steel\model.ifc";   // @ = verbatim: \ is literalConsole.WriteLine(path);
Output
Line 1
Line 2
She said "Hi"
C:\Projects\Steel\model.ifc

Inside a string: indexes and methods

Interactive

Inside a string

A string is a row of characters, each with an index that starts at 0. Edit the text and try the methods.

␣ = space · text.Length = 19

text.Substring(9, 2)"C#"

Takes 2 characters starting at index 9.

MemberWhat it does
s.Lengthnumber of characters
s[i]the character at index i (a char)
s.ToUpper() / s.ToLower()change case
s.Trim()remove spaces at both ends
s.Contains(x)true if x appears in s
s.IndexOf(x)position of x, or -1
s.Substring(start, length)part of the string
s.Replace(a, b)replace every a with b
s.Split(sep)cut into a string[]
string.IsNullOrWhiteSpace(s)true for null, empty or only spaces

Comparing strings

Program.cs
string a = "Steel";string b = "steel"; Console.WriteLine(a == b);                                              // FalseConsole.WriteLine(a.Equals(b, StringComparison.OrdinalIgnoreCase));     // True
Output
False
True
Quick check

What does "Hello".Substring(1, 3) return?

Quick check

After string s = "hi"; s.ToUpper(); what is s?

Practice

Ask the user for their full name (e.g. “ ravi selva ”). Print it trimmed and in capitals, then print how many characters it has (without the outer spaces), and the first letter.

Show the solution
Solution.cs
Console.Write("Full name: ");string name = (Console.ReadLine() ?? "").Trim(); Console.WriteLine(name.ToUpper());Console.WriteLine($"Length: {name.Length}");if (name.Length > 0)    Console.WriteLine($"First letter: {name[0]}");
Output
Full name:   ravi selva
RAVI SELVA
Length: 10
First letter: r

Remember

  • Console.ReadLine() returns a string; convert it for numbers.
  • Prefer interpolation: $"Hello {name}", with formats like {x:F2}.
  • Indexes start at 0; the last index is Length - 1.
  • Strings are immutable: store the result of ToUpper, Trim, Replace…
0 of 60 lessons completed