DEV Community

Cover image for C# Strings Explained: Methods, Formatting, and Manipulation
suraj kumar
suraj kumar

Posted on

C# Strings Explained: Methods, Formatting, and Manipulation

When working with C#, one of the most commonly used data types is the C# string. Strings are everywhere—whether you're displaying a user’s name, storing an email address, or formatting output for a report. Understanding how to work with strings effectively is an essential skill for any C# developer.

In this post, we’ll explore what strings are, how they work in memory, and how to use important string methods, formatting techniques, and manipulation operations in C#.

What is a String in C#?

In C#, a string is a sequence of characters. It’s represented by the System.String class and can hold text data like words, sentences, or even numbers as text.
You can create a string simply by enclosing characters inside double quotes:

string message = "Hello, World!";
Enter fullscreen mode Exit fullscreen mode

Behind the scenes, the string type in C# is an object, not a primitive data type. This means it comes with built-in methods and properties you can use to manipulate and analyze text.

Strings Are Immutable

Before diving into manipulation, it’s important to understand that strings in C# are immutable.
This means once a string is created, its value cannot be changed in memory. If you modify a string, C# actually creates a new string in memory instead of altering the original one.

For example:

string name = "Suraj";
name += " Kumar";
Console.WriteLine(name);
Enter fullscreen mode Exit fullscreen mode

Here, "Suraj" and "Kumar" form a new string "Suraj Kumar". The original "Suraj" string remains unchanged.
This immutability makes string operations safe but can affect performance in cases where a lot of modifications are required — in such cases, using the StringBuilder class is more efficient.

Common String Methods in C#

C# provides a rich set of methods to work with strings. Let’s look at some of the most commonly used ones:

  1. Length

Returns the number of characters in a string.

string city = "Noida";
Console.WriteLine(city.Length); // Output: 5
Enter fullscreen mode Exit fullscreen mode
  1. ToUpper() and ToLower()

Used to convert text to uppercase or lowercase.

string text = "Hello";
Console.WriteLine(text.ToUpper()); // HELLO
Console.WriteLine(text.ToLower()); // hello
Enter fullscreen mode Exit fullscreen mode
  1. Substring()

Extracts a portion of a string based on the starting index.

string word = "Programming";
string part = word.Substring(0, 7);
Console.WriteLine(part); // Program
Enter fullscreen mode Exit fullscreen mode
  1. Contains()

Checks if a string contains another substring.

string sentence = "C# is fun!";
Console.WriteLine(sentence.Contains("fun")); // True
Enter fullscreen mode Exit fullscreen mode
  1. Replace()

Replaces all occurrences of a character or substring with another.

string data = "C# is easy";
string result = data.Replace("easy", "powerful");
Console.WriteLine(result); // C# is powerful
Enter fullscreen mode Exit fullscreen mode
  1. Trim()

Removes whitespace from the start and end of a string.

string name = "  Suraj Kumar  ";
Console.WriteLine(name.Trim()); // "Suraj Kumar"
Enter fullscreen mode Exit fullscreen mode
  1. Split() and Join()

Used for breaking a string into parts and joining them back.

string names = "Amit, Raj, Neha";
string[] list = names.Split(',');
foreach (string n in list)
{
    Console.WriteLine(n.Trim());
}
Enter fullscreen mode Exit fullscreen mode

And you can join them back:

string combined = string.Join(" | ", list);
Console.WriteLine(combined); // Amit | Raj | Neha
Enter fullscreen mode Exit fullscreen mode

String Formatting in C#

Formatting strings helps make your output more readable and structured. C# supports several ways to format strings:

1. Using String Concatenation

string first = "Suraj";
string last = "Kumar";
string fullName = first + " " + last;
Console.WriteLine(fullName);
Enter fullscreen mode Exit fullscreen mode

2. Using String.Format()

string name = "Ravi";
int age = 25;
string info = string.Format("Name: {0}, Age: {1}", name, age);
Console.WriteLine(info);
Enter fullscreen mode Exit fullscreen mode

3. String Interpolation (Recommended)

Introduced in C# 6.0, string interpolation provides a cleaner syntax.

string user = "Anjali";
int score = 89;
Console.WriteLine($"Student {user} scored {score} marks.");
Enter fullscreen mode Exit fullscreen mode

4. Composite Formatting

You can also format numbers, dates, or currencies.

double price = 1999.99;
Console.WriteLine($"Price: {price:C}");
Enter fullscreen mode Exit fullscreen mode

Output:

Price: ₹1,999.99
Enter fullscreen mode Exit fullscreen mode

Manipulating Strings

You can perform various manipulations such as reversing, searching, or comparing strings.

1. Reversing a String

string word = "CSharp";
char[] arr = word.ToCharArray();
Array.Reverse(arr);
string reversed = new string(arr);
Console.WriteLine(reversed); // prahSC
Enter fullscreen mode Exit fullscreen mode

2. Comparing Strings

C# offers different methods for comparison.

string a = "hello";
string b = "Hello";
Console.WriteLine(a.Equals(b)); // False
Console.WriteLine(a.Equals(b, StringComparison.OrdinalIgnoreCase)); // True
Enter fullscreen mode Exit fullscreen mode

3. Checking Start or End

string file = "data.txt";
Console.WriteLine(file.StartsWith("data")); // True
Console.WriteLine(file.EndsWith(".txt"));   // True
Enter fullscreen mode Exit fullscreen mode

Using StringBuilder for Better Performance

When you perform many concatenations, use StringBuilder from the System.Text namespace. It avoids creating multiple string objects in memory.

using System.Text;

StringBuilder sb = new StringBuilder();
sb.Append("Learning ");
sb.Append("C# ");
sb.Append("Strings");
Console.WriteLine(sb.ToString());
Enter fullscreen mode Exit fullscreen mode

StringBuilder is mutable, which means it modifies data in the same memory location — ideal for loops or dynamic text generation.

Conclusion

Strings are one of the most powerful and frequently used data types in C#. Understanding how they work and how to use built-in methods can make your code cleaner, faster, and easier to maintain. Whether you’re formatting output, comparing data, or manipulating text, C# provides every tool you need through its String class and StringBuilder.

By mastering these string operations, you’ll find everyday programming tasks simpler and more efficient — and you’ll write C# code that’s both elegant and powerful.

Top comments (1)

Collapse
 
pauljlucas profile image
Paul J. Lucas

The #c tag is exclusive for C, not C#. Please don't tag C# with #c.