在C#列表中的所有字符串中删除特定的char [英] Remove specific char in all strings in a list in c#

查看:212
本文介绍了在C#列表中的所有字符串中删除特定的char的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在列表中有一些字符串

I have some strings in a list they are

KHIJEFGACDB
KHIJEFGBACD
KHIJEFGBCDA
KHIJEFGCDAB
KHIJEFGCDBA
KHIJGABCDEF
KHIJGABEFCD
KHIJGACDBEF
KHIJGACDEFB
KHIJGAEFBCD
KHIJGAEFCDB
KHIJGBACDEF
KHIJGBAEFCD

我需要删除列表中所有字符串中可用的HIJ.

I need to remove HIJ which is available in all strings in the list.

我制作了一个如下的C#程序

I made a C# program like below

foreach (string item in items)
{
    item.Replace("HIJ", "");
}
Console.WriteLine(items.FirstOrDefault().Length);

但是它仍然显示11,这意味着HIJ没有被删除.如何解决这个问题并得到8作为答案.

But its still displaying 11 that means the HIJ is not removed. How to solve this and get 8 as the answer.

推荐答案

字符串在C#中是不可变的.试试

Strings are immutable in C#. Try

for (int i = 0; i < items.Length; i++)
    items[i] = items[i].Replace("HIJ", "");

也请注意

foreach (string item in items)

item无法修改.而且,顺便说一句,foreach 不能用于在源集合中添加或删除项目,以避免不可预测的副作用.请参阅 http://msdn.microsoft.com/en-us/library/ttw7t8t6. aspx .

item cannot be modified. And, BTW, foreach can not be used to add or remove items from the source collection to avoid unpredictable side effects. See http://msdn.microsoft.com/en-us/library/ttw7t8t6.aspx.

这篇关于在C#列表中的所有字符串中删除特定的char的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆