0

How to replace a set of characters where I only know the first and the last one, in between is a variable that is not constant.

All I know is that this string will always start with & and it will end with ;

string str = "Hello &145126451; mate!"; 

How to get rid of &145126451; ?

So the desired result is:

string result = "Hello  mate!"
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
threesixnine
  • 1,733
  • 7
  • 30
  • 56

1 Answers1

5

The most easiest way is to use Regex:

    Regex yourRegex = new Regex(@"&.*;");
    string result = yourRegex.Replace("Hello &145126451; mate!", String.Empty);
    Console.WriteLine(result);

Here is a fiddle with example.

teo van kot
  • 12,350
  • 10
  • 38
  • 70