朋友们,
我有一个场景,我必须使用c#代码从价格中删除无效字符。
我希望常规的ex删除这个角色或某些东西。
我的价格是
“3,950,000(例如税)”
我想从价格中删除“(例如TAX)”。
我的情景就是这样。我必须删除字符串中的任何字符,除了数字,点(。)和逗号(,)
请帮助..
提前致谢
Shivi
答案 0 :(得分:23)
private string RemoveExtraText(string value)
{
var allowedChars = "01234567890.,";
return new string(value.Where(c => allowedChars.Contains(c)).ToArray());
}
答案 1 :(得分:1)
这个怎么样:
using System.Text.RegularExpressions;
public static Regex regex = new Regex(
"(\\d|[,\\.])*",
RegexOptions.IgnoreCase
| RegexOptions.CultureInvariant
| RegexOptions.IgnorePatternWhitespace
| RegexOptions.Compiled
);
//// Capture the first Match, if any, in the InputText
Match m = regex.Match(InputText);
//// Capture all Matches in the InputText
MatchCollection ms = regex.Matches(InputText);
//// Test to see if there is a match in the InputText
bool IsMatch = regex.IsMatch(InputText);
答案 2 :(得分:1)
string s = @"3,950,000 ( Ex. TAX )";
string result = string.Empty;
foreach (var c in s)
{
int ascii = (int)c;
if ((ascii >= 48 && ascii <= 57) || ascii == 44 || ascii == 46)
result += c;
}
Console.Write(result);
请注意,“Ex.TAX”中的点将保留
答案 3 :(得分:0)
您可以使用LINQ
HashSet<char> validChars = new HashSet<char>(
new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', ',', '.' });
var washedString = new string((from c in "3,950,000 ( Ex. TAX )"
where validChars.Contains(c)
select c).ToArray());
但是“。”在“Ex.TAX”中将保留。
答案 4 :(得分:0)
你可以使用[^ alpha] ore [^ a-z]
之类的东西