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
  • 429,027
  • 67
  • 649
  • 891
Naughty Ninja
  • 1,674
  • 7
  • 22
  • 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,154
  • 10
  • 38
  • 67