我有这个方法,它接受一个字符串,只从中获取数字并修剪空格,但它不起作用:
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;
}
如果白色空格位于数字之间,如何剪裁字符串,如屁股603 123 4321 ???
答案 0 :(得分:1)
此外,为什么需要更换空格? 例如,您可以将其更改为:
input = Regex.Replace(input, "[^0-9']", ""); - becomes "6239750".
请注意:此正则表达式将删除所有字符,包括空格。
答案 1 :(得分:0)
尝试使用:
string subjectString = "OFF6239750";
string resultString = null;
try {
resultString = Regex.Match(subjectString, @"\d+").Value;
} catch (ArgumentException ex) {
// Syntax error in the regular expression
}