Manipulating and processing strings is a vital skill in any programming language. In C#, one common task is removing specific characters from a string. In this tutorial, we will see, how to remove a particular character from string C#.net with a simple and a complex example.
Remove a particular character from string C#.net
Let us see the process of removing a particular character from a string in C#.NET.
Step 1: Set Up Your Environment
Before you start, you’ll need a development environment. You can use any text editor for writing C# code, but Visual Studio is recommended because it provides powerful tools for .NET development. You can download it from the official Microsoft website.
Step 2: Create a New Console Application
Once you have Visual Studio installed and running:
- Go to
File
>New
>Project
. - Select
Console App (.NET Core)
and clickNext
. - Enter a name and location for your project, then click
Create
.
Step 3: Write the Code
Here’s a simple standalone program that removes the letter ‘a’ from the string “Atlanta”:
using System;
class Program
{
static void Main()
{
string cityName = "Atlanta";
char charToRemove = 'a';
string result = cityName.Replace(charToRemove.ToString(), string.Empty);
Console.WriteLine(result); // prints "Atlnt"
}
}
In this program:
- We define a string
cityName
that contains the name “Atlanta”. - We define a character
charToRemove
that we want to remove from the string. - We call the
Replace
method oncityName
, passingcharToRemove.ToString()
, andstring.Empty
as arguments. TheReplace
method replaces all instances of the first argument with the second argument. Since we want to remove the character, we replace it with an empty string. - We print the result to the console. The output will be “Atlnt”, which is “Atlanta” with all the ‘a’ characters removed.
You can see the output:

Example-2:
Here is another example:
using System;
namespace RemoveCharacter
{
class Program
{
static void Main(string[] args)
{
string[] cityNames = {"Chicago", "Los Angeles", "Atlanta", "Houston", "Seattle"};
foreach (string cityName in cityNames)
{
string modifiedCityName = RemoveCharacter(cityName, 'a');
Console.WriteLine("Original: " + cityName + ", Modified: " + modifiedCityName);
}
Console.ReadLine();
}
static string RemoveCharacter(string str, char charToRemove)
{
return str.Replace(charToRemove.ToString(), string.Empty);
}
}
}
This code does the following:
- It defines an array of city names.
- It loops through each city name, removing the character ‘a’ from each city name by calling the
RemoveCharacter
method. - It prints the original city name and the modified city name.
- The
RemoveCharacter
method takes a string and a character as parameters, and it uses theReplace
method to replace all occurrences of the character with an empty string.
Step 4: Run the Code
You can run your application by clicking the Start Debugging
button or pressing F5
. The application will display the original and modified city names in the console window.
Conclusion
This is how to remove a particular character from a string in C#.NET. This code can be modified to remove different characters or to work with different strings. It’s a simple but effective way to manipulate strings in C#.NET.
You may also like:
- Remove First and Last Character from StringBuilder in C#.NET
- Get the First and Last Letter of a String in C#.NET
- How to get first character of each word in string c#.net?
- How to Compare String with Multiple Values in C#?
Bijay Kumar is a renowned software engineer, accomplished author, and distinguished Microsoft Most Valuable Professional (MVP) specializing in SharePoint. With a rich professional background spanning over 15 years, Bijay has established himself as an authority in the field of information technology. He possesses unparalleled expertise in multiple programming languages and technologies such as ASP.NET, ASP.NET MVC, C#.NET, and SharePoint, which has enabled him to develop innovative and cutting-edge solutions for clients across the globe. Read more…