c# remove characters from string code example
Example 1: c# remove character from string at index
string s = "This is string";
s = s.Remove(2, 1);
string s = "This is string";
s = s.Remove(2, 2);
Example 2: c# remove specific character from string
string phrase = "this is, a string with, too many commas";
phrase = phrase.Replace(",", "");
Example 3: C# copy string except for last letter
using System;
class Program
{
static void Main()
{
string input = "OneTwoThree";
string sub = input.Substring(0, input.Length - 5);
Console.WriteLine("Substring: {0}", sub);
}
}
Output
Substring: OneTwo
Example 4: c# Remove String In C#
string founder = "Mahesh Chand is a founder of C# Corner";
string first25 = founder.Remove(25);
Console.WriteLine(first25);
String newStr = founder.Remove(10, 12);
Console.WriteLine(newStr);
Example 5: remove specific character from string c#
string input = "1, 2, 55";
string[] inputSplit = input.Split(", ");
Example 6: remove control characters from string c#
string input;
string output = new string(input.Where(c => !char.IsControl(c)).ToArray());