从字符串数组替换字符串的所有出现(Replace all occurences of a strin

2019-06-26 17:17发布

我有一个字符串数组,如:

 string [] items = {"one","two","three","one","two","one"};

我想一次性替换零所有的人。 然后,项目应该是:

{"zero","two","three","zero","two","zero"};

我发现了一个解决方案如何在一个字符串数组替换项目? 。

但它只会替换第一次出现。 这是最好的方法/更换所有出现?

Answer 1:

世界上没有办法做到这一点不循环。即使是这样的内部循环:

string [] items = {"one","two","three","one","two","one"};

string[] items2 = items.Select(x => x.Replace("one", "zero")).ToArray();

我不知道为什么你的要求是,你不能循环。然而,它会永远需要循环。



Answer 2:

还有就是要取代它,而无需通过每个元素循环的一种方式:

 string [] items = {"zero","two","three","zero","two","zero"};

除此之外,你必须通过数组迭代(用于/λ/的foreach)



Answer 3:

对不起,你必须循环。 有没有绕开的机会。

此外,所有其他的答案给你想要的元素的数组 。 如果你想在同一阵列有它的元素修饰,你的问题意味着,你应该做这样。

for (int index = 0; index < items.Length; index++)
    if (items[index] == "one")
        items[index] = "zero";

简单。

为了避免每次你需要做到这一点,创建一个方法时间写在你的代码的循环:

void ReplaceAll(string[] items, string oldValue, string newValue)
{
    for (int index = 0; index < items.Length; index++)
        if (items[index] == oldValue)
            items[index] = newValue;
}

然后调用它是这样的:

ReplaceAll(items, "one", "zero");

您也可以把它变成一个扩展方法:

static class ArrayExtensions
{
    public static void ReplaceAll(this string[] items, string oldValue, string newValue)
    {
        for (int index = 0; index < items.Length; index++)
            if (items[index] == oldValue)
                items[index] = newValue;
    }
}

然后,你可以这样调用:

items.ReplaceAll("one", "zero");

当你在这,你可能想使它通用:

static class ArrayExtensions
{
    public static void ReplaceAll<T>(this T[] items, T oldValue, T newValue)
    {
        for (int index = 0; index < items.Length; index++)
            if (items[index].Equals(oldValue))
                items[index] = newValue;
    }
}

呼叫网站看起来相同。

现在,这些方法都没有支持自定义字符串平等检查。 例如,你可能想的比较是大小写敏感的,还是不行。 添加过载,其采用IEqualityComparer<T>所以可以提供你喜欢的比较; 这是更加灵活,是否Tstring或别的东西:

static class ArrayExtensions
{
    public static void ReplaceAll<T>(this T[] items, T oldValue, T newValue)
    {
        items.ReplaceAll(oldValue, newValue, EqualityComparer<T>.Default);
    }

    public static void ReplaceAll<T>(this T[] items, T oldValue, T newValue, IEqualityComparer<T> comparer)
    {
        for (int index = 0; index < items.Length; index++)
            if (comparer.Equals(items[index], oldValue))
                items[index] = newValue;
    }
}


Answer 4:

你也可以做到并行:

Parallel.For(0, items.Length,
  idx => { if(items[idx] == "one") { item[idx] = "zero"; } });


Answer 5:

string [] items = {"one","two","three","one","two","one"};
items =  items.Select(s => s!= "one" ? s : "zero").ToArray();

从找到答案在这里 。



Answer 6:

你可以试试这个,但我认为,它会做循环也。

string [] items = {"one","two","three","one","two","one"};
var str= string.Join(",", items);
var newArray = str.Replace("one","zero").Split(new char[]{','});


Answer 7:

string[] items = { "one", "two", "three", "one", "two", "one" };

如果你想把它当作你指定的索引方式:

int n=0;
while (true)
{
n = Array.IndexOf(items, "one", n);
if (n == -1) break;
items[n] = "zero";
}

但LINQ会更好

var lst = from item in items
select item == "one" ? "zero" : item;


Answer 8:

string[] newarry = items.Select(str => { if (str.Equals("one")) str = "zero"; return str; }).ToArray();


文章来源: Replace all occurences of a string from a string array