← C# from Scratch Lesson 11 of 39 · 10 XP

Strings

Theory and problem statement

Machine translation, not yet reviewed by a native speaker.

A string is an immutable sequence of characters. Its methods return a new string:

string s = "Hello World";
s.Length              // 11
s.ToUpper()           // "HELLO WORLD"
s.ToLower()           // "hello world"
s.IndexOf("World")    // 6, or -1 if it's not there
s.Substring(6)        // "World"
s.Substring(0, 5)     // "Hello" — from position 0, length 5
s[0]                  // 'H' — the character at an index (a char)
s.Contains("lo")      // true
s.Replace("World", "IGD")
s.Trim()              // without spaces at the edges
string.Join("-", new[] {"a", "b"})   // "a-b"

You can join strings with +, with $"..." interpolation or with string.Concat. For many joins in a loop, use StringBuilder (each + creates a new string). Strings are compared with == — by content.

Task. Read a full name of the form First Last. On the first line print the initials with dots (A. K.), on the second the last name in capital letters, and on the third the length of the whole name without the space.

Sign up for free to solve this task right in your browser: code editor, running, test checks and hints.