Trim 函数没有去除字符串中的空格

Trim Function not getting rid of white spaces in string

我有这个方法,它接受一个字符串,只从中获取数字和 trim 空格,但它不起作用:

 public static string ToNumericOnlyFAX(this string input)
   {
    if (input == "OFF6239750")
     {
      input = Regex.Replace(input, "[^0-9']", " "); - becomes "   6239750"
      input.TrimStart().Trim().TrimEnd(); - after any of these its still the above, I want it to be "6239750" 

        if (input.Length <= 10 && input == "6239750") - if the above works  then I can padleft
         {
         input.PadLeft(10, '0');
           }
         }
         return input; 
        }

如何 trim 如果空格位于数字之间,如 ass 603 123 4321???

尝试使用:

string subjectString = "OFF6239750";
string resultString = null;
try {
    resultString = Regex.Match(subjectString, @"\d+").Value;
} catch (ArgumentException ex) {
    // Syntax error in the regular expression
}

另外,为什么要用空格替换? 例如,您可以将其更改为:

input = Regex.Replace(input, "[^0-9']", ""); - becomes "6239750".

请注意:此正则表达式将删除 所有 个字符,包括空格。