我如何从数组中删除任何字符串,而只有整数
string[] result = col["uncheckedFoods"].Split(',');
Run Code Online (Sandbox Code Playgroud)
我有
[0] = on; // remove this string
[1] = 22;
[2] = 23;
[3] = off; // remove this string
[4] = 24;
Run Code Online (Sandbox Code Playgroud)
我想要
[0] = 22;
[1] = 23;
[2] = 24;
Run Code Online (Sandbox Code Playgroud)
我试过了
var commaSepratedID = string.Join(",", result);
var data = Regex.Replace(commaSepratedID, "[^,0-9]+", string.Empty);
Run Code Online (Sandbox Code Playgroud)
但是在第一个元素之前有一个逗号,有没有更好的方法来删除字符串?
fub*_*ubo 11
这将选择可以解析为的所有字符串 int
string[] result = new string[5];
result[0] = "on"; // remove this string
result[1] = "22";
result[2] = "23";
result[3] = "off"; // remove this string
result[4] = "24";
int temp;
result = result.Where(x => int.TryParse(x, out temp)).ToArray();
Run Code Online (Sandbox Code Playgroud)