C# Strings


A string is a sequence of characters used to represent text. The string type in C# is an alias for the System.String class in the .NET Framework. Strings in C# are immutable, meaning once a string object is created, it cannot be modified. Any operation that appears to modify a string will actually create a new string.

Key Features of C# Strings

  1. Immutability: Once a string is created, it cannot be changed. Any method that appears to modify a string will return a new string.

  2. String Interpolation: C# supports string interpolation, allowing you to embed expressions within string literals.

  3. Escape Sequences: Strings can contain special characters using escape sequences, such as \n for a new line, \t for a tab, and \\ for a backslash.

  4. String Concatenation: You can concatenate strings using the + operator or String.Concat method.

  5. String Methods: The String class provides numerous methods for string manipulation, including searching, replacing, and formatting.

Detailed Examples

1. Creating Strings

csharp
1string greeting = "Hello, World!"; 2string emptyString = ""; // An empty string 3string multiLineString = @"This is a multi-line 4string using verbatim syntax.";

2. String Interpolation

csharp
1string name = "Alice"; 2int age = 30; 3string message = $"My name is {name} and I am {age} years old."; 4Console.WriteLine(message); // Output: My name is Alice and I am 30 years old.

3. Escape Sequences

csharp
1string path = "C:\\Program Files\\MyApp"; // Using \\ to escape backslash 2string quote = "He said, \"Hello!\""; // Using \" to include quotes 3string newLine = "First Line\nSecond Line"; // Using \n for new line 4Console.WriteLine(newLine);

4. String Concatenation

csharp
1string firstName = "John"; 2string lastName = "Doe"; 3string fullName = firstName + " " + lastName; // Concatenation using + 4Console.WriteLine(fullName); // Output: John Doe

5. Common String Methods

csharp
1string text = "Hello, World!"; 2int length = text.Length; // Gets the length of the string 3string upper = text.ToUpper(); // Converts to uppercase 4string lower = text.ToLower(); // Converts to lowercase 5bool contains = text.Contains("World"); // Checks if it contains "World" 6string replaced = text.Replace("World", "C#"); // Replaces "World" with "C#" 7string substring = text.Substring(7, 5); // Gets a substring starting at index 7, length 5 8 9Console.WriteLine(length); // Output: 13 10Console.WriteLine(upper); // Output: HELLO, WORLD! 11Console.WriteLine(lower); // Output: hello, world! 12Console.WriteLine(contains); // Output: True 13Console.WriteLine(replaced); // Output: Hello, C#! 14Console.WriteLine(substring); // Output: World

6. String Formatting

csharp
1int quantity = 5; 2double price = 19.99; 3string formatted = string.Format("You have {0} items costing {1:C}.", quantity, price); 4Console.WriteLine(formatted); // Output: You have 5 items costing $19.99.

7. Splitting and Joining Strings

csharp
1string csv = "apple,banana,cherry"; 2string[] fruits = csv.Split(','); // Splits the string into an array 3string joined = string.Join(" and ", fruits); // Joins the array into a single string 4Console.WriteLine(joined); // Output: apple and banana and cherry

A string is a sequence of characters used to represent text. The string type in C# is an alias for the System.String class in the .NET Framework. Strings in C# are immutable, meaning once a string object is created, it cannot be modified. Any operation that appears to modify a string will actually create a new string.

Key Features of C# Strings

  1. Immutability: Once a string is created, it cannot be changed. Any method that appears to modify a string will return a new string.

  2. String Interpolation: C# supports string interpolation, allowing you to embed expressions within string literals.

  3. Escape Sequences: Strings can contain special characters using escape sequences, such as \n for a new line, \t for a tab, and \\ for a backslash.

  4. String Concatenation: You can concatenate strings using the + operator or String.Concat method.

  5. String Methods: The String class provides numerous methods for string manipulation, including searching, replacing, and formatting.

Detailed Examples

1. Creating Strings

csharp
1string greeting = "Hello, World!"; 2string emptyString = ""; // An empty string 3string multiLineString = @"This is a multi-line 4string using verbatim syntax.";

2. String Interpolation

csharp
1string name = "Alice"; 2int age = 30; 3string message = $"My name is {name} and I am {age} years old."; 4Console.WriteLine(message); // Output: My name is Alice and I am 30 years old.

3. Escape Sequences

csharp
1string path = "C:\\Program Files\\MyApp"; // Using \\ to escape backslash 2string quote = "He said, \"Hello!\""; // Using \" to include quotes 3string newLine = "First Line\nSecond Line"; // Using \n for new line 4Console.WriteLine(newLine);

4. String Concatenation

csharp
1string firstName = "John"; 2string lastName = "Doe"; 3string fullName = firstName + " " + lastName; // Concatenation using + 4Console.WriteLine(fullName); // Output: John Doe

5. Common String Methods

csharp
1string text = "Hello, World!"; 2int length = text.Length; // Gets the length of the string 3string upper = text.ToUpper(); // Converts to uppercase 4string lower = text.ToLower(); // Converts to lowercase 5bool contains = text.Contains("World"); // Checks if it contains "World" 6string replaced = text.Replace("World", "C#"); // Replaces "World" with "C#" 7string substring = text.Substring(7, 5); // Gets a substring starting at index 7, length 5 8 9Console.WriteLine(length); // Output: 13 10Console.WriteLine(upper); // Output: HELLO, WORLD! 11Console.WriteLine(lower); // Output: hello, world! 12Console.WriteLine(contains); // Output: True 13Console.WriteLine(replaced); // Output: Hello, C#! 14Console.WriteLine(substring); // Output: World

6. String Formatting

csharp
1int quantity = 5; 2double price = 19.99; 3string formatted = string.Format("You have {0} items costing {1:C}.", quantity, price); 4Console.WriteLine(formatted); // Output: You have 5 items costing $19.99.

7. Splitting and Joining Strings

csharp
1string csv = "apple,banana,cherry"; 2string[] fruits = csv.Split(','); // Splits the string into an array 3string joined = string.Join(" and ", fruits); // Joins the array into a single string 4Console.WriteLine(joined); // Output: apple and banana and cherry

Talk to us?

Post your blog