String in C#

To repeat a character in C#, you can use the new string(char c, int count) method. For example, to repeat the character '*' 10 times, you can use:

string repeatedChar = new string('*', 10);

To repeat a string in C#, you can use a for loop or the string.Concat method. For example, to repeat the string "hello" 3 times, you can use:

string originalString = "hello";
int repeatCount = 3;

// Using a for loop
string repeatedString = "";
for (int i = 0; i < repeatCount; i++)
{
    repeatedString += originalString;
}

// Using string.Concat
string repeatedString = string.Concat(Enumerable.Repeat(originalString, repeatCount));

To concatenate strings in C#, you can use the + operator, the string.Concat method, or the StringBuilder class. For example, to concatenate the strings "hello" and "world", you can use:

string str1 = "hello";
string str2 = "world";

// Using the + operator
string concatenatedString = str1 + " " + str2;

// Using string.Concat
string concatenatedString = string.Concat(str1, " ", str2);

// Using StringBuilder
StringBuilder sb = new StringBuilder();
sb.Append(str1);
sb.Append(" ");
sb.Append(str2);
string concatenatedString = sb.ToString();

To check if a string is a palindrome in C#, you can reverse the string and compare it to the original string. For example:

string str = "racecar";
string reversedStr = new string(str.Reverse().ToArray());

bool isPalindrome = str.Equals(reversedStr); // true

To convert a char[] to a string in C#, you can use the string constructor that takes a char[]. For example:

char[] charArray = { 'h', 'e', 'l', 'l', 'o' };
string str = new string(charArray); // "hello"

To read a string line by line in C#, you can use the StreamReader class. For example:

using (StreamReader reader = new StreamReader("file.txt"))
{
    string line;
    while ((line = reader.ReadLine()) != null)
    {
        Console.WriteLine(line);
    }
}
  1. C# string concatenation:

    string str1 = "Hello";
    string str2 = " World";
    string result = str1 + str2;
    // Result: "Hello World"
    
  2. Substring in C# string:

    string original = "Hello World";
    string substring = original.Substring(6, 5);
    // Result: "World"
    
  3. String interpolation in C#:

    string name = "John";
    int age = 30;
    string message = $"My name is {name} and I am {age} years old.";
    // Result: "My name is John and I am 30 years old."
    
  4. C# string format examples:

    string formatted = string.Format("Name: {0}, Age: {1}", "John", 30);
    // Result: "Name: John, Age: 30"
    
  5. Trimming whitespace in C# strings:

    string withWhitespace = "   Hello   ";
    string trimmed = withWhitespace.Trim();
    // Result: "Hello"
    
  6. C# string comparison methods:

    string str1 = "apple";
    string str2 = "orange";
    int comparisonResult = string.Compare(str1, str2);
    // Result: -1 (str1 comes before str2)
    
  7. Parsing numbers from strings in C#:

    string numberString = "123";
    int parsedNumber = int.Parse(numberString);
    // Result: 123
    
  8. Replacing characters in C# strings:

    string original = "Hello World";
    string replaced = original.Replace("World", "Universe");
    // Result: "Hello Universe"
    
  9. Splitting strings in C#:

    string names = "John,Jane,Jim";
    string[] nameArray = names.Split(',');
    // Result: ["John", "Jane", "Jim"]
    
  10. Checking if a string contains a substring in C#:

    string sentence = "The quick brown fox";
    bool containsWord = sentence.Contains("fox");
    // Result: true
    
  11. Converting strings to uppercase or lowercase in C#:

    string original = "Hello";
    string upper = original.ToUpper();
    string lower = original.ToLower();
    // Result: upper="HELLO", lower="hello"
    
  12. Regular expressions with strings in C#:

    using System.Text.RegularExpressions;
    
    string pattern = @"\d+";
    string input = "123 abc";
    Match match = Regex.Match(input, pattern);
    // Result: match.Value="123"
    
  13. C# string length and size:

    string text = "Hello";
    int length = text.Length;
    // Result: length=5
    
  14. Joining strings in C# with StringBuilder:

    StringBuilder builder = new StringBuilder();
    builder.Append("Hello");
    builder.Append(" World");
    string result = builder.ToString();
    // Result: "Hello World"
    
  15. Encoding and decoding strings in C#:

    string original = "Hello World";
    byte[] encodedBytes = Encoding.UTF8.GetBytes(original);
    string decodedString = Encoding.UTF8.GetString(encodedBytes);
    // Result: decodedString="Hello World"
    
  16. C# verbatim string literals:

    string path = @"C:\Users\John\Documents";
    // No need to escape backslashes in verbatim string literals