Codectionary / Developer documentation / C#

String Methods

The string class provides a large set of built-in methods for searching, transforming, splitting, and validating text. Common ones include Split() and Join() for converting between a string and an array, Replace() for substitution, and a family of Is-prefixed static helpers like string.IsNullOrEmpty() for validation. Since strings are immutable, every transforming method returns a new string.

Syntax

string.MethodName(arguments)

Examples

Splitting and Joining

Converting between a string and an array of strings.

string csv = "Fola,Zain,Jamal";
string[] names = csv.Split(',');

foreach (string name in names)
{
    Console.WriteLine(name);
}

string joined = string.Join(" & ", names);
Console.WriteLine(joined);  // Fola & 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

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

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

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

At a glance

Purpose
Applications on the .NET platform
File extension
.cs
Runs in
.NET runtime
Usually used with
.NET SDK and libraries

Specifications & further reading

Related C# documentation

Variables & Data Types
C# is a statically-typed language, meaning every variable's type is fixed at compile time. Built-in value types include int, double, decimal, bool, and char, while string and object are reference types. The var keyword lets the compiler infer a variable's type from its initializer, but the variable is still strongly typed underneath - it just saves you from writing the type name explicitly.
Console.WriteLine / ReadLine
The Console class, from the System namespace, provides the standard way to read from and write to the terminal in a C# console application. Console.WriteLine() prints a value followed by a newline, Console.Write() prints without one, and Console.ReadLine() reads a full line of text typed by the user, always returning it as a string.
Comments & XML Documentation
C# supports single-line comments with //, multi-line comments with /* */, and a special triple-slash XML documentation format (///) placed above a member. XML doc comments support tags like <summary>, <param>, and <returns>, and are used by Visual Studio and other IDEs to power IntelliSense tooltips and can be compiled into full API documentation.
Arithmetic & Assignment Operators
C# provides the standard arithmetic operators for numeric computation: +, -, *, / for division, and % for the remainder. Integer division truncates any decimal part, just as in many C-family languages. Compound assignment operators (+=, -=, etc.) combine an operation with assignment, and increment/decrement operators (++, --) come in prefix and postfix forms that differ subtly in when the value updates relative to being used.