You could use LINQ. The code below filters the string into an IEnumerable with only digits and then converts it to a char[]. The string constructor can then convert the char[] into a string:
string a = "557222]]>";
string b = "5100870
";
a = new string(a.Where(x => char.IsDigit(x)).ToArray());
b = new string(b.Where(x => char.IsDigit(x)).ToArray());