string greeting = "Hello";
string name = "Alice";
string message = greeting + "," + name + "!";
Console.WriteLine(message); // Hello, Alice
Console.WriteLine($"Length of name: {name.Length}"); // string length
Console.WriteLine($"To Upper: {name.ToUpper()}"); // convert to uppercase
Console.WriteLine($"Substring: {name.Substring(1)}"); // part of string
string text = "C# is awesome!";
Console.WriteLine($"Contains 'awesome': {text.Contains("awesome")}");
Console.WriteLine($"Starts with 'C#': {text.StartsWith("C#")}");
Console.WriteLine($"Index of 'is': {text.IndexOf("is")}");
The StringBuilder class in C# is part of the System.Text namespace and is used for efficiently manipulating and building strings. Unlike regular strings, which are immutable (meaning modifying a string creates a new one), a StringBuilder allows you to modify strings without creating new objects each time, improving performance in scenarios where you need to build or modify strings frequently.
StringBuilder sb = new StringBuilder("Hello");
sb.Append(","); // add
sb.Append("World!");
Console.WriteLine(sb.ToString()); // Hello,World!