Replace all occurences of a string from a string array

前端 未结 8 1399
伪装坚强ぢ
伪装坚强ぢ 2020-12-11 16:49

I have a string array like:

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

I would like to replace all ones with z

相关标签:
8条回答
  • 2020-12-11 17:10
    string[] items = { "one", "two", "three", "one", "two", "one" };
    

    If you want it the index way as you specified:

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

    But LINQ would be better

    var lst = from item in items
    select item == "one" ? "zero" : item;
    
    0 讨论(0)
  • 2020-12-11 17:20

    Sorry, you have to loop. There's no getting around it.

    Also, all of the other answers give you a new array with the desired elements. If you want the same array to have its elements modified, as your question implies, you should just do it like this.

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

    Simple.

    To avoid writing a loop in your code every time you need this to happen, create a method:

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

    Then call it like this:

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

    You can also make it into an extension method:

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

    Then you can call it like this:

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

    While you're at it, you might want to make it generic:

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

    The call site looks the same.

    Now, none of these approaches supports custom string equality checking. For example, you might want the comparison to be case sensitive, or not. Add an overload that takes an IEqualityComparer<T>, so you can supply the comparison you like; this is much more flexible, whether T is string or something else:

    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;
        }
    }
    
    0 讨论(0)
提交回复
热议问题