c# 从数组中删除字符串

标签 c# arrays string

我如何从数组中删除任何字符串并只使用整数

string[] result = col["uncheckedFoods"].Split(',');

我有

[0] = on;      // remove this string
[1] = 22;
[2] = 23;
[3] = off;      // remove this string
[4] = 24;

我要

[0] = 22;
[1] = 23;
[2] = 24;

我试过了

var commaSepratedID = string.Join(",", result);

var data = Regex.Replace(commaSepratedID, "[^,0-9]+", string.Empty);

但是第一个元素之前有一个逗号,有没有更好的方法来删除字符串?

最佳答案

这将选择所有可以解析为 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();

关于c# 从数组中删除字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40628430/

相关文章:

c# - 使用ADSI检索本地组成员所需的权限

c - 全局数组在 while 循环内不更新?

html - Angular 2 中的字符串最佳实践

java - 在字符串中查找姐妹括号

c# - UI 自动化 - #32770(对话框)在 Insepct.exe 中显示,但不在 VisualUIAVerifyNative.exe 中显示

c# - 是否可以检查未保存的函数的返回值?

arrays - 如何获得 Array.remove(at :) in Swift) 的 O(1) 时间复杂度

php - PHP 中单引号和双引号字符串有什么区别?

c# - SqlDependency 代码监听来自 Windows 服务的表中的插入/更新

arrays - 如何使Split不在每个元素的末尾添加换行符