Hello World

Hello World

Console.WriteLine("Hello World!");

Declare and use variables

// Declare variable
string aFriend = "Bill";
Console.WriteLine(aFriend);

// Assignment other value to existing variable
aFriend = "Maria";
Console.WriteLine(aFriend);

// Concatenation
Console.WriteLine("Hello " + aFriend);

// String interpolation (f-string in Python)
Console.WriteLine($"Hello {aFriend}");

Work with strings

// A string with leading and trailing spaces
string greeting = "      Hello World!       ";
Console.WriteLine($"{greeting}");

// Trim trailing spaces
string trimmedGreeting = greeting.TrimStart();
Console.WriteLine($"[{trimmedGreeting}]");

// Trim leading spaces
trimmedGreeting = greeting.TrimEnd();
Console.WriteLine($"[{trimmedGreeting}]");

// Trim both leading and trailing spaces
trimmedGreeting = greeting.Trim();
Console.WriteLine($"[{trimmedGreeting}]");

// Search and replace
string sayHello = "Hello World!";
Console.WriteLine(sayHello);
sayHello = sayHello.Replace("Hello", "Greetings");
Console.WriteLine(sayHello);

// Upper and lower
Console.WriteLine(sayHello.ToUpper());
Console.WriteLine(sayHello.ToLower());

Search strings

// Search in string
string songLyrics = "You say goodbye, and I say hello";
Console.WriteLine(songLyrics.Contains("goodbye"));
Console.WriteLine(songLyrics.Contains("greetings"));

// StartsWith and EndsWith
Console.WriteLine(songLyrics.StartsWith("You"));
Console.WriteLine(songLyrics.EndsWith("hello"));

Last updated