C# 替换字符串数组中出现的所有字符串

C# 替换字符串数组中出现的所有字符串,c#,arrays,string,replace,C#,Arrays,String,Replace,我有一个字符串数组,如: string [] items = {"one","two","three","one","two","one"}; 我想立即用零替换所有的。 那么项目应该是: {“零”、“二”、“三”、“零”、“二”、“零”} 我找到了一个解决办法 但它只会取代第一次出现的情况。替换所有事件的最佳方法/途径是什么?没有循环就无法做到这一点。。甚至像这样的东西也会在内部循环: string [] items = {"one","two","three","one","two","o

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

 string [] items = {"one","two","three","one","two","one"};
我想立即用零替换所有的。 那么项目应该是:

{“零”、“二”、“三”、“零”、“二”、“零”}

我找到了一个解决办法


但它只会取代第一次出现的情况。替换所有事件的最佳方法/途径是什么?

没有循环就无法做到这一点。。甚至像这样的东西也会在内部循环:

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

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

我不知道为什么你的要求是不能循环。。但是,它总是需要循环。

您可以尝试这个,但我认为,它也可以循环

string[] newarry = items.Select(str => { if (str.Equals("one")) str = "zero"; return str; }).ToArray();
string [] items = {"one","two","three","one","two","one"};
var str= string.Join(",", items);
var newArray = str.Replace("one","zero").Split(new char[]{','});
如果您希望按照指定的索引方式执行此操作:

int n=0;
while (true)
{
n = Array.IndexOf(items, "one", n);
if (n == -1) break;
items[n] = "zero";
}
但是林克会更好

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

有一种方法可以在不通过每个元素的情况下替换它:

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

除此之外,还必须遍历数组(for/lambda/foreach)

对不起,必须循环。这是绕不开的

另外,所有其他答案都会为您提供一个包含所需元素的新数组。如果您想修改同一数组的元素,正如您的问题所暗示的,您应该这样做

for (int index = 0; index < items.Length; index++)
    if (items[index] == "one")
        items[index] = "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;
    }
}
当您使用它时,您可能希望使其通用:

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

从中找到答案。

您也可以并行执行:

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

你关心数组中元素的顺序吗?我想知道如何在不访问项目的情况下进行替换?@Anirudha想详细解释一下“那些优化”的含义吗?如果项目包含“noone”,它将被替换为“nozero”,rt?我需要准确地替换“一”。@Praveen:那么你原来的问题是错的?您不想在没有循环的情况下执行此操作,也不想用零替换所有出现的1?您真正的问题是“如何在字符串数组中替换仅包含字母“1”的字符串,而不使用for/foreach/dowhile循环?”这是否正确?请注意,这会创建一个新数组。哎哟。。有更好的解决方案吗?如果您对数组有其他引用,那么保留原始数组可能很重要,否则您的解决方案当然可以,但它会在内部循环两次。一次用于计算数组长度的项数,一次用于替换。见phoog的答案。
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;
    }
}
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;
    }
}
string [] items = {"one","two","three","one","two","one"};
items =  items.Select(s => s!= "one" ? s : "zero").ToArray();
Parallel.For(0, items.Length,
  idx => { if(items[idx] == "one") { item[idx] = "zero"; } });