C# 将一个字符串数组复制到另一个

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/886488/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 08:25:49  来源:igfitidea点击:

Copy one string array to another

c#arrayscopy

提问by Arunachalam

How can I copy a string[]from another string[]?

如何string[]从另一个复制一个string[]

Suppose I have string[] args. How can I copy it to another array string[] args1?

假设我有string[] args. 如何将其复制到另一个数组string[] args1

采纳答案by sharptooth

Allocate space for the target array, that use Array.CopyTo():

为使用 Array.CopyTo() 的目标数组分配空间:

targetArray = new string[sourceArray.Length];
sourceArray.CopyTo( targetArray, 0 );

回答by Jon Skeet

  • To create a completely new array with the same contents (as a shallow copy): call Array.Cloneand just cast the result.
  • To copy a portion of a string array into another string array: call Array.Copyor Array.CopyTo
  • 要创建一个具有相同内容的全新数组(作为浅拷贝):调用Array.Clone并只转换结果。
  • 要将字符串数组的一部分复制到另一个字符串数组中:调用Array.CopyArray.CopyTo

For example:

例如:

using System;

class Test
{
    static void Main(string[] args)
    {
        // Clone the whole array
        string[] args2 = (string[]) args.Clone();

        // Copy the five elements with indexes 2-6
        // from args into args3, stating from
        // index 2 of args3.
        string[] args3 = new string[5];
        Array.Copy(args, 2, args3, 0, 5);

        // Copy whole of args into args4, starting from
        // index 2 (of args4)
        string[] args4 = new string[args.Length+2];
        args.CopyTo(args4, 2);
    }
}

Assuming we start off with args = { "a", "b", "c", "d", "e", "f", "g", "h" }the results are:

假设我们从args = { "a", "b", "c", "d", "e", "f", "g", "h" }结果开始:

args2 = { "a", "b", "c", "d", "e", "f", "g", "h" }
args3 = { "c", "d", "e", "f", "g" }
args4 = { null, null, "a", "b", "c", "d", "e", "f", "g", "h" } 

回答by stt106

The above answers show a shallow clone; so I thought I add a deep clone example using serialization; of course a deep clone can also be done by looping through the original array and copy each element into a brand new array.

上面的答案显示了一个浅层克隆;所以我想我使用序列化添加了一个深度克隆示例;当然,深度克隆也可以通过循环遍历原始数组并将每个元素复制到一个全新的数组中来完成。

 private static T[] ArrayDeepCopy<T>(T[] source)
        {
            using (var ms = new MemoryStream())
            {
                var bf = new BinaryFormatter{Context = new StreamingContext(StreamingContextStates.Clone)};
                bf.Serialize(ms, source);
                ms.Position = 0;
                return (T[]) bf.Deserialize(ms);
            }
        }

Testing the deep clone:

测试深度克隆:

 private static void ArrayDeepCloneTest()
        {
            //a testing array
            CultureInfo[] secTestArray = { new CultureInfo("en-US", false), new CultureInfo("fr-FR") };

            //deep clone
            var secCloneArray = ArrayDeepCopy(secTestArray);

            //print out the cloned array
            Array.ForEach(secCloneArray, x => Console.WriteLine(x.DateTimeFormat.DateSeparator));

            //modify the original array
            secTestArray[0].DateTimeFormat.DateSeparator = "-";

            Console.WriteLine();
            //show the (deep) cloned array unchanged whereas a shallow clone would reflect the change...
            Array.ForEach(secCloneArray, x => Console.WriteLine(x.DateTimeFormat.DateSeparator));
        }