Strings in C#
1. What is a String?
- A string is a sequence of characters stored as text.
- In C, string is a built-in data type.
2. Declaring a String
- Syntax: string variableName;
- Example: string message;
3. Initializing a String
- You can assign a value directly:
string message = "Hello, World!";
4. Accessing String Characters
- Strings are indexed, starting at 0.
Console.WriteLine(message[0]); // Outputs: H
5. String Concatenation
- Combine strings using + or String.Concat.
string fullName = "John" + " " + "Doe";
6. String Interpolation
- Use $ to embed variables into strings:
string name = "Alice";
string greeting = $"Hello, {name}!";
7. Finding String Length
- Use the Length property:
Console.WriteLine(message.Length);
8. Comparing Strings
- Use == or String.Equals.
if (string1 == string2) { ... }
9. Changing Case
- Convert to uppercase or lowercase:
string upper = message.ToUpper();
string lower = message.ToLower();
10. Substring
- Extract part of a string:
string sub = message.Substring(0, 5); // "Hello"
1. Declare and Print a String
string message = "Hello, World!";
Console.WriteLine(message); // Outputs: Hello, World!
2. Find Length of a String
string message = "Hello";
Console.WriteLine(message.Length); // Outputs: 5
3. Concatenate Two Strings
string firstName = "John";
string lastName = "Doe";
string fullName = firstName + " " + lastName;
Console.WriteLine(fullName); // Outputs: John Doe
4. Convert to Uppercase
string message = "hello";
Console.WriteLine(message.ToUpper()); // Outputs: HELLO
5. Convert to Lowercase
string message = "HELLO";
Console.WriteLine(message.ToLower()); // Outputs: hello
6. Check if String Contains a Word
string message = "Hello, World!";
if (message.Contains("World"))
{
Console.WriteLine("Found");
}
// Outputs: Found
7. Compare Two Strings
string str1 = "Hello";
string str2 = "hello";
if (str1.Equals(str2, StringComparison.OrdinalIgnoreCase))
{
Console.WriteLine("Strings are equal");
}
// Outputs: Strings are equal
8. Extract a Substring
string message = "Hello, World!";
string sub = message.Substring(7, 5);
Console.WriteLine(sub); // Outputs: World
9. Reverse a String
string message = "Hello";
char[] chars = message.ToCharArray();
Array.Reverse(chars);
string reversed = new string(chars);
Console.WriteLine(reversed); // Outputs: olleH
10. Split a String
string message = "Hello,World,Here";
string[] words = message.Split(',');
foreach (string word in words)
{
Console.WriteLine(word);
}
// Outputs: Hello
// World
// Here
11. Trim a String
string message = " Hello ";
Console.WriteLine(message.Trim()); // Outputs: Hello
12. Replace Characters in a String
string message = "Hello, World!";
string updated = message.Replace("World", "C");
Console.WriteLine(updated); // Outputs: Hello, C!
13. Check if String Starts With
string message = "Hello, World!";
if (message.StartsWith("Hello"))
{
Console.WriteLine("Yes");
}
// Outputs: Yes
14. Check if String Ends With
string message = "Hello, World!";
if (message.EndsWith("!"))
{
Console.WriteLine("Ends with !");
}
// Outputs: Ends with !
15. Convert String to Char Array
string message = "Hello";
char[] chars = message.ToCharArray();
foreach (char c in chars)
{
Console.WriteLine(c);
}
// Outputs: H e l l o
16. Join Strings
string[] words = { "Hello", "World" };
string message = string.Join(" ", words);
Console.WriteLine(message); // Outputs: Hello World
17. Check If String is Empty
string message = "";
if (string.IsNullOrEmpty(message))
{
Console.WriteLine("String is empty");
}
// Outputs: String is empty
18. Convert Number to String
int number = 123;
string str = number.ToString();
Console.WriteLine(str); // Outputs: 123
19. Convert String to Number
string str = "123";
int number = int.Parse(str);
Console.WriteLine(number); // Outputs: 123
20. Repeat a String
string message = "Hi ";
string repeated = string.Concat(Enumerable.Repeat(message, 3));
Console.WriteLine(repeated); // Outputs: Hi Hi Hi