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

标签 c# arrays copy

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

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

最佳答案

  • 要创建一个具有相同内容的全新数组(作为浅拷贝):调用 Array.Clone并只是投出结果。
  • 要将字符串数组的一部分复制到另一个字符串数组中:调用 Array.CopyArray.CopyTo

例如:

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

假设我们从 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" } 

关于c# - 将一个字符串数组复制到另一个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/886488/

相关文章:

javascript - 需要帮助在 ASP.NET Web 窗体中使用 jQuery 遍历元素

c# - Windows窗体应用程序从串口接收数据

c# - 如何复制 C# 3D 锯齿状数组

java - 简化 Java 中多种方法的代码?

javascript - JS/jQuery : Copying Contents of td to clipboard

Python 获取 SettingWithCopyWarning - iloc 与 loc - 无法弄清楚原因

c# - Entity Framework 6 Guid 生成空?

c# - Func<T,TResult> 是如何工作的?

javascript - 如何判断玩家距离敌人是否 2 个格子

Java 复制和粘贴文件 NoSuchFileException