C# String Examples
C# Basic
C# Data Type
C# OOP
C# IO
C# Graphics & UI
C# Advanced
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); } }
C# string concatenation:
string str1 = "Hello"; string str2 = " World"; string result = str1 + str2; // Result: "Hello World"
Substring in C# string:
string original = "Hello World"; string substring = original.Substring(6, 5); // Result: "World"
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."
C# string format examples:
string formatted = string.Format("Name: {0}, Age: {1}", "John", 30); // Result: "Name: John, Age: 30"
Trimming whitespace in C# strings:
string withWhitespace = " Hello "; string trimmed = withWhitespace.Trim(); // Result: "Hello"
C# string comparison methods:
string str1 = "apple"; string str2 = "orange"; int comparisonResult = string.Compare(str1, str2); // Result: -1 (str1 comes before str2)
Parsing numbers from strings in C#:
string numberString = "123"; int parsedNumber = int.Parse(numberString); // Result: 123
Replacing characters in C# strings:
string original = "Hello World"; string replaced = original.Replace("World", "Universe"); // Result: "Hello Universe"
Splitting strings in C#:
string names = "John,Jane,Jim"; string[] nameArray = names.Split(','); // Result: ["John", "Jane", "Jim"]
Checking if a string contains a substring in C#:
string sentence = "The quick brown fox"; bool containsWord = sentence.Contains("fox"); // Result: true
Converting strings to uppercase or lowercase in C#:
string original = "Hello"; string upper = original.ToUpper(); string lower = original.ToLower(); // Result: upper="HELLO", lower="hello"
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"
C# string length and size:
string text = "Hello"; int length = text.Length; // Result: length=5
Joining strings in C# with StringBuilder:
StringBuilder builder = new StringBuilder(); builder.Append("Hello"); builder.Append(" World"); string result = builder.ToString(); // Result: "Hello World"
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"
C# verbatim string literals:
string path = @"C:\Users\John\Documents"; // No need to escape backslashes in verbatim string literals