Home > front end >  C# Delete between two spesific character
C# Delete between two spesific character

Time:06-07

Lets think we have string like below :

I tried using split but it didn't work. I don't know where the exclamation point will be.

string test = "I am writing something here!Developer is trying something! bla bla bla 
              I am continue!Here is trying! developing."

I wanna get output like :

I am writing something here bla bla bla I am continue developing.

Basically, I want to delete between two exclamation mark.

CodePudding user response:

To remove the content between the exclamation marks, you can use a regex to replace !{anything but ! many times}! by an empty string:

var input = "I am writing something here!Developer is trying something! I am continue!Here is trying! developing."
var regex = new Regex("![^!]*!");
var output = regex.Replace(input, string.Empty); // "I am writing something here I am continue developing."

CodePudding user response:

You can delete specific characters from a string by using the string.Replace method, which takes in two characters - the character you want to replace and the character you want to replace it with.

test.Replace('!', '');

https://docs.microsoft.com/en-us/dotnet/api/system.string.replace?view=net-6.0#system-string-replace(system-char-system-char)

CodePudding user response:

        string text = "I am writing something here!Developer is trying something! bla bla #bla I am continue!Here #is trying! developing.";
        
        // Replaces only ! marks in your code
        string output1 = Regex.Replace(text, @"[!] ", "") ;
        
        // Replaces ! and # - If you need more please add as or condition
        string output2 = Regex.Replace(text, @"[!|#] ", "") ;
  • Related