我有一个字符串,其中包含许多字符。我想删除A-Za-z和空格,只留下其余部分。最好的方法是什么?
这是我尝试过的方法:
presaleEstimateHigh = Regex.Replace(presaleEstimateHigh, @"[A-Za-z]", string.Empty);
但我也需要去除空格。
presaleEstimateHigh = Regex.Replace(presaleEstimateHigh, @"[A-Za-z\s]", string.Empty);
没有正则表达式:
var chars = str.Where(c => !char.IsLetter(c) && !char.IsWhitespace(c)).ToArray();
var rest = new string(chars);
string result = Regex.Replace(myString, @"[a-zA-Z\s]+", string.Empty);
[a-zA-Z ]+
它只包括空格。添加一个+
使其更有效率,因为整个字符序列可以一次性替换(在内部)。
%20
不是唯一的空格。 - mellamokb