Replace all occurrences of a string from an array of strings

I have a string array like:

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

I would like to replace everything with zero immediately. Then the points should be:

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

I found one solution. How to replace an element in an array of strings? .

But he will replace only the first occurrence. What is the best method / approach to replace all occurrences?

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

Found answer from here .

+3
source

It is impossible to do this without loops .. even something like this loops inside:

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

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

I am not sure why your requirement is that you cannot go in cycles. However, he will always need a cycle.

+30
source

:

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

, (/lambda/foreach)

+14

, . .

, . , , , .

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

Simple.

, , :

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>, , ; , T string - :

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;
    }
}
+8

:

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

, , .

string [] items = {"one","two","three","one","two","one"};
var str= string.Join(",", items);
var newArray = str.Replace("one","zero").Split(new char[]{','});
+1
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;
+1
string[] newarry = items.Select(str => { if (str.Equals("one")) str = "zero"; return str; }).ToArray();
0

All Articles