Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/261.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
“要删除”;0“;在列表中的索引0处,但不是10,20,30。。。。。C#_C#_Regex_Replace - Fatal编程技术网

“要删除”;0“;在列表中的索引0处,但不是10,20,30。。。。。C#

“要删除”;0“;在列表中的索引0处,但不是10,20,30。。。。。C#,c#,regex,replace,C#,Regex,Replace,我的代码 但这会删除所有0 如果我输入0、10、100 我只想删除第一个0(index0作为列表排序),但我不能用表达式表示它应该一直删除index0,因为我取决于,如果用户输入0。。。所以它应该在第一个索引中查找0,并且(仅)如果它在那里。。。移除它 我不能对这件事耿耿于怀 提前感谢我不知道为什么需要正则表达式和字符上的循环。以下是三种方法: List<int> a = new List<int> { 0, 10, 20, 5 }; List<int> b

我的代码

但这会删除所有0 如果我输入0、10、100 我只想删除第一个0(index0作为列表排序),但我不能用表达式表示它应该一直删除index0,因为我取决于,如果用户输入0。。。所以它应该在第一个索引中查找0,并且(仅)如果它在那里。。。移除它

我不能对这件事耿耿于怀


提前感谢

我不知道为什么需要正则表达式和字符上的循环。以下是三种方法:

List<int> a = new List<int> { 0, 10, 20, 5 };
List<int> b = new List<int> { 0, 10, 20 };

//Option 1 - .Remove() on one of the lists
a.Remove(0);
var matches = a.Intersect(b);

//Option 2 - .Remove() on intersection
var matches = a.Intersect(b).ToList();
matches.Remove(0);

//Option 3 - .Where()
var matches = a.Where(item => item != 0)
               .Intersect(b);

var text = string.Join(",", matches); //For both ways: 10,20
List a=新列表{0,10,20,5};
列表b=新列表{0,10,20};
//选项1-.Remove()位于其中一个列表上
a、 移除(0);
var匹配=a.相交(b);
//选项2-.Remove()位于交叉点
var matches=a.Intersect(b.ToList();
匹配。删除(0);
//选项3-。其中()
var matches=a.Where(item=>item!=0)
.相交(b);
var text=string.Join(“,”匹配)//双向:10,20

您能显示示例数据吗?。为什么不直接使用
匹配
?将它们转换为字符串,然后尝试解析它们的原因是什么?循环会迭代
输出中的字符,这肯定不是您想要的。然而,你为什么不干脆用
list.RemoveAt()
删除列表中的第一个元素呢?你想从用户输入中删除前导零吗?@HimBromBeere-虽然我没有想到前导零不会发生,因为他与
int
的列表相交,非常感谢你。。。我选择了var matches=a.Intersect(b.ToList();匹配。删除(0);而且它工作得很好!
var match = a.Intersect(b);
string output = string.Join(",", match);
foreach (var x in output)
{
     label.Text += Regex.Replace(Convert.ToString(x), "[0]", "");
}
List<int> a = new List<int> { 0, 10, 20, 5 };
List<int> b = new List<int> { 0, 10, 20 };

//Option 1 - .Remove() on one of the lists
a.Remove(0);
var matches = a.Intersect(b);

//Option 2 - .Remove() on intersection
var matches = a.Intersect(b).ToList();
matches.Remove(0);

//Option 3 - .Where()
var matches = a.Where(item => item != 0)
               .Intersect(b);

var text = string.Join(",", matches); //For both ways: 10,20