c# remove char from string code example

Example 1: remove all text after string c#

string input = "text?here";
int index = input.LastIndexOf("?"); // Character to remove "?"
if (index > 0)
    input = input.Substring(0, index); // This will remove all text after character ?

Example 2: c# remove word from string

public static string RemoveHTML(string text)
{
    text = text.Replace(" ", " ").Replace("<br>", "\n").Replace("description", "").Replace("INFRA:CORE:", "")
        .Replace("RESERVED", "")
        .Replace(":", "")
        .Replace(";", "")
        .Replace("-0/3/0", "");
        var oRegEx = new System.Text.RegularExpressions.Regex("<[^>]+>");
        return oRegEx.Replace(text, string.Empty);
}

Example 3: c# remove character from string at index

string s = "This is string";
s = s.Remove(2, 1);
//Output: Ths is string


string s = "This is string";
s = s.Remove(2, 2);
//Output: Th is string

Example 4: c# remove specific character from string

string phrase = "this is, a string with, too many commas";
phrase = phrase.Replace(",", "");

Example 5: 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 6: remove specific character from string c#

string input = "1, 2, 55";

//This will split the string for everything that is in between ", "
string[] inputSplit = input.Split(", ");

//items in inputSplit = "1" "2" "55"