Scenario: Download Script
You are working as C# or dot net developer. You are writing a program in which you need to remove a character from string.
Let's take real time example, you are reading file names which comes underscore "_". You want to remove all the underscores from file name.
In this example we used Replace Method. We find the character "_" and then replace with ""( no space).
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TechBrothersIT.com_CSharp_Tutorial
{
class Program
{
staticvoid Main(string[] args)
{
//Declare File Name variable and save value
string FileName = "my_text_file_20160202.txt";
//create variable FileNameRemoved and save value of FileName variable after removing _
string FileNameRemoved = FileName.Replace("_", "");
//Print the variable values
Console.WriteLine("FileName Variable Value: " + FileName);
Console.WriteLine("FileNameRemoved Variable Value: " + FileNameRemoved);
//If you want to remove _ and save to same variable
FileName = FileName.Replace("_", "");
Console.WriteLine("FileName Variable Value after removing _:" + FileName);
Console.ReadLine();
}
}
}
How to remove character from string in C# |