using System; using System.Text; // Required for StringBuilder class Program { static void Main() { // --- String Class Methods --- Console.WriteLine("=== String Class Methods ==="); string str = "Hello World"; Console.WriteLine("Original String: " + str); Console.WriteLine("To Upper Case: " + str.ToUpper()); Console.WriteLine("To Lower Case: " + str.ToLower()); Console.WriteLine("Substring (0,5): " + str.Substring(0, 5)); Console.WriteLine("Replaced 'World' with 'C#': " + str.Replace("World", "C#")); Console.WriteLine("Contains 'Hello'? " + str.Contains("Hello")); Console.WriteLine("Length: " + str.Length); // --- StringBuilder Class Methods --- Console.WriteLine("\n=== StringBuilder Class Methods ==="); StringBuilder sb = new StringBuilder("Hello"); sb.Append(" World"); Console.WriteLine("After Append: " + sb); sb.Insert(5, " C#"); Console.WriteLine("After Insert: " + sb); sb.Replace("World", "Developers"); Console.WriteLine("After Replace: " + sb); sb.Remove(5, 3); Console.WriteLine("After Remove: " + sb); sb.Clear(); sb.Append("New String"); Console.WriteLine("After Clear and Append: " + sb); } }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter