Read the syntax
Instance methods such as text.Split and text.Replace act on a string value and return a result; they do not mutate the original string. Static methods such as string.Join combine values supplied as arguments.
Indexes are zero-based, so IndexOf reports the first matching position starting at 0. Formatting and padding control display width, not the underlying numeric value.
Syntax
string.MethodName(arguments)Examples
Splitting and Joining
Converting between a string and an array of strings.
string csv = "John,Zain,Jamal";
string[] names = csv.Split(',');
foreach (string name in names)
{
Console.WriteLine(name);
}
string joined = string.Join(" & ", names);
Console.WriteLine(joined); // John & Zain & Jamal- Converting between a string and an array of strings.
- Instance methods such as text.Split and text.Replace act on a string value and return a result; they do not mutate the original string. Static methods such as string.Join combine values supplied as arguments.
- Split creates three names at the commas, foreach prints them one per line, and Join combines that array into John & Zain & Jamal.
Searching and Replacing
Finding content within a string and performing substitution.
string sentence = "The quick brown fox";
Console.WriteLine(sentence.Contains("quick")); // true
Console.WriteLine(sentence.IndexOf("brown")); // 10
Console.WriteLine(sentence.Replace("fox", "dog")); // The quick brown dog
Console.WriteLine(sentence.StartsWith("The")); // true- Finding content within a string and performing substitution.
- Instance methods such as text.Split and text.Replace act on a string value and return a result; they do not mutate the original string. Static methods such as string.Join combine values supplied as arguments.
- Contains and StartsWith return true for this sentence; IndexOf finds brown at position 10. Replace returns The quick brown dog without changing the original sentence.
Validation Helpers
Static helper methods for common validation checks on strings.
string empty = "";
string whitespace = " ";
string value = "hello";
Console.WriteLine(string.IsNullOrEmpty(empty)); // true
Console.WriteLine(string.IsNullOrWhiteSpace(whitespace)); // true
Console.WriteLine(string.IsNullOrEmpty(value)); // false- Static helper methods for common validation checks on strings.
- Instance methods such as text.Split and text.Replace act on a string value and return a result; they do not mutate the original string. Static methods such as string.Join combine values supplied as arguments.
- IsNullOrEmpty detects the empty value, while IsNullOrWhiteSpace also treats three spaces as blank. The nonempty value returns false for IsNullOrEmpty.
PadLeft, PadRight, and Formatting Numbers
Aligning text output with padding, useful for tabular console output.
string label = "Score";
Console.WriteLine(label.PadRight(10) + "|" + "95".PadLeft(5));
int number = 7;
Console.WriteLine(number.ToString("D3")); // 007- Aligning text output with padding, useful for tabular console output.
- Instance methods such as text.Split and text.Replace act on a string value and return a result; they do not mutate the original string. Static methods such as string.Join combine values supplied as arguments.
- PadRight and PadLeft align the label and 95 around a separator in console text. D3 formats the integer 7 as 007; number remains the integer 7.
Best practices
- Use string.IsNullOrEmpty() or string.IsNullOrWhiteSpace() instead of manually checking for null and empty separately
- Use string.Join() instead of a manual loop with string concatenation when combining array elements into one string
- Chain string methods when it improves readability (input.Trim().ToLower()), but break into steps once a chain gets too long to read at a glance
- Use StringComparison.OrdinalIgnoreCase explicitly for case-insensitive comparisons rather than lowercasing both sides first
Runtime & versions
.NET runtime. Feature availability depends on the language and runtime version used by your project; consult the official reference below.
C# language reference