Essential String Manipulation Functions in C#

In C#, the string type is an alias for System.String. String literals must be enclosed in double quotes.

string siteAddress = "www.devsiki.com";

To obtain the length of a string, use the Length property.

int charCount = siteAddress.Length;

String equality can be checked using the == operator.

if (siteAddress == "www.devsiki.com")
{
    Console.Write("Strings match");
}
else
{
    Console.Write("Strings do not match");
}

Strings are immutable. Concatenation creates a new string object.

siteAddress = "http://" + siteAddress; // Creates a new string

Individual characters can be accessed via an indexer, starting at 0.

char fourthChar = siteAddress[3]; // Returns 'p' from "http"

The CompareTo method compares string content lexicographically, returning 0 for equality, -1 if the instance precedes the argument, or 1 if it follows.

string baseUrl = "www.devsiki.com";
int comparisonResult = baseUrl.CompareTo("www.devsiki.com"); // Returns 0

Use Replace to substitute characters or substrings.

string modifiedUrl = baseUrl.Replace(".", "----"); // "www----devsiki----com"

The Split method divides a string into an array based on a delimiter.

string[] urlParts = baseUrl.Split('.'); // {"www", "devsiki", "com"}

Substring extracts a portion of the string starting at a specified endex.

string domainPart = baseUrl.Substring(4); // "devsiki.com"

Convert casee using ToUpper or ToLower.

string upperCaseUrl = baseUrl.ToUpper(); // "WWW.DEVSIKI.COM"

Remove leading and trailing whitespace with Trim.

string paddedString = " www.devsiki.com ";
string cleanString = paddedString.Trim(); // "www.devsiki.com"

Find the first occurrence of a substring with IndexOf, which returns -1 if not found.

int position = baseUrl.IndexOf("devsiki"); // Returns 4

The String.Format method is used for formatting numeric and date/time values.

Numeric Formatting Examples:

// Two decimal places
String.Format("{0:0.00}", 123.4567); // "123.46"

// Maximum two decimal places
String.Format("{0:0.##}", 123.4);    // "123.4"

// Thousands separator
String.Format("{0:0,0.0}", 12345.67); // "12,345.7"

// Alignment
String.Format("{0,10:0.0}", 123.4567);  // "     123.5"
String.Format("{0,-10:0.0}", 123.4567); // "123.5     "

DateTime Formatting Examples:

DateTime sampleDate = new DateTime(2008, 3, 9, 16, 5, 7, 123);

String.Format("{0:y yy yyy yyyy}", sampleDate); // "8 08 008 2008"
String.Format("{0:M/d/yyyy}", sampleDate);      // "3/9/2008"
String.Format("{0:MM/dd/yyyy}", sampleDate);    // "03/09/2008"
String.Format("{0:dddd, MMMM d, yyyy}", sampleDate); // "Sunday, March 9, 2008"
String.Format("{0:t}", sampleDate); // "4:05 PM"
String.Format("{0:T}", sampleDate); // "4:05:07 PM"

Tabular Output Example:

Console.WriteLine("-------------------------------");
Console.WriteLine("First Name | Last Name  |   Age");
Console.WriteLine("-------------------------------");
Console.WriteLine(String.Format("{0,-10} | {1,-10} | {2,5}", "Bill", "Gates", 51));
Console.WriteLine(String.Format("{0,-10} | {1,-10} | {2,5}", "Edna", "Parker", 114));
Console.WriteLine(String.Format("{0,-10} | {1,-10} | {2,5}", "Johnny", "Depp", 44));
Console.WriteLine("-------------------------------");

Output:

-------------------------------
First Name | Last Name  |   Age
-------------------------------
Bill       | Gates      |    51
Edna       | Parker     |   114
Johnny     | Depp       |    44
-------------------------------

Tags: C# string programming .NET formatting

Posted on Sat, 16 May 2026 02:20:35 +0000 by son.of.the.morning