Skip to content

String manipulations in C#

Lost in coding? Discover our Learning Paths!
Lost in coding? Discover our Learning Paths!

Intro

Welcome to our article on string manipulation in C# – a fundamental topic for any C# programmer. In this guide, we’ll cover everything you need to know to manipulate strings effectively, from the basic concepts of string manipulation to advanced techniques. By the end of this article, you’ll be equipped with the knowledge and skills to confidently work with strings in C# and take your programming abilities to the next level. So, whether you’re a beginner or an experienced developer looking to brush up your string manipulation skills, let’s dive in and explore the world of string manipulation in C#.

Concatenation

String concatenation is the process of combining two or more strings into a single string. In C#, you can concatenate strings using the + operator, the += operator, or the string.Concat() method.

Here are some examples:

Using “+” operator:

string str1 = "Hello";
string str2 = "World";
string result = str1 + " " + str2;
Console.WriteLine(result); // Output: Hello World
Using “+” operator:
string str1 = "Hello";
string str2 = "World";
str1 += " " + str2;
Console.WriteLine(str1); // Output: Hello World
Using “string.Concat()” method:
string str1 = "Hello";
string str2 = "World";
string result = string.Concat(str1, " ", str2);
Console.WriteLine(result); // Output: Hello World

You can also concatenate variables of different data types by converting them to strings first using the ToString() method. For example:

int num = 10; // we used int but it might be any dt
string str = "The number is " + num.ToString();
Console.WriteLine(str); // Output: The number is 10

Note that string concatenation can be inefficient when working with large numbers of strings or when concatenating strings in a loop. In such cases, consider using the StringBuilder class, which is optimized for string concatenation.

 

String formatting

String formatting is the process of creating a formatted string by substituting values into a string template. In C#, you can format strings using composite formatting, string interpolation, or the string.Format() method.

Composite Formatting

Composite formatting is the most common way to format strings in C#. It involves creating a string template with placeholders that are replaced by the values you want to substitute.

Here’s an example:

string name = "John";
int age = 30;
string result = string.Format("My name is {0} and I'm {1} years old.", name, age);
Console.WriteLine(result); // Output: My name is John and I'm 30 years old.

 

In the string template, the placeholders {0} and {1} are replaced by the values of the name and age variables, respectively. You can also specify formatting options for each placeholder using format specifiers. For example:

double price = 123.45;
string result = string.Format("The price is {0:C}", price);
Console.WriteLine(result); // Output: The price is $123.45

 

In this example, the format specifier :C formats the price variable as a currency value.

String Interpolation

String interpolation is a newer way to format strings in C# that was introduced in C# 6.0. It provides a more concise and readable syntax for creating formatted strings.

Here’s an example:

string name = "John";
int age = 30;
string result = $"My name is {name} and I'm {age} years old.";
Console.WriteLine(result); // Output: My name is John and I'm 30 years old.

 

In the string template, the placeholders {name} and {age} are replaced by the values of the name and age variables, respectively. You can also specify formatting options for each placeholder using format specifiers, just like in composite formatting.

The string.Format() Method

The string.Format() method is an alternative to composite formatting that can be used to create formatted strings. It works by taking a string template and an array of values to substitute.

Here’s an example:

string name = "John";
int age = 30;
string result = string.Format("My name is {0} and I'm {1} years old.", new object[] { name, age });
Console.WriteLine(result); // Output: My name is John and I'm 30 years old.

 

In this example, the string.Format() method takes a string template and an array of objects that correspond to the placeholders in the string template. The placeholders are replaced with the values in the array.

 

If you want to skyrocket your C# career, check out our powerful ASP.NET FULL-STACK WEB DEVELOPMENT COURSE, which also covers test-driven development and C# software architecture.

Lost in coding? Discover our Learning Paths!
Lost in coding? Discover our Learning Paths!
Tired of being just an average developer?
Stop wasting your time and learn coding the right (and easy) way!
Tired of being just an average developer?
Stop wasting your time and learn coding the right (and easy) way!
Enter your email and we will send you the PDF guide:
Enter your email and we will send you the PDF guide