【问题标题】:Copy string array to an existing string array in C# [duplicate]将字符串数组复制到C#中的现有字符串数组[重复]
【发布时间】:2015-03-23 22:03:21
【问题描述】:
 String[] search1 = Directory.EnumerateFiles(voiceSource, callId + " "+ separator +"*."+ fileFormat +"")
                         .Where(file => Path.GetFileName(file).StartsWith( callId + " "+ separator +""))
                         .Select(path => Path.GetFileName(path))
                         .ToArray();

现在我想将下面的数组复制到上面的search1中。换句话说,我想合并两个数组。

  String[] search2 = Directory.EnumerateFiles(voiceSource, callId + ""+ separator +"*."+ fileFormat +"")
                         .Where(file => Path.GetFileName(file).StartsWith(callId + ""+ separator +""))
                         .Select(path => Path.GetFileName(path))
                         .ToArray();

请注意,search1 的长度可能为零。有什么帮助吗?

【问题讨论】:

  • 我不太清楚你想要达到什么目的 - 你能澄清一下吗?
  • 你的意思是你只想连接两个数组?如果是这样,请使用 LINQ:a.Concat(b).ToArray()

标签: c# arrays


【解决方案1】:
search3 = new type[search1.Length + search2.Length];
search1.CopyTo(search3, 0);
search2.CopyTo(search3, search1.Length);

其中type是数组中元素的类型

来自How do I concatenate two arrays in C#?

【讨论】:

  • 不要从另一个答案复制代码,而是标记为另一个问题的重复。
猜你喜欢
  • 2015-10-25
  • 1970-01-01
  • 2016-01-05
  • 2020-11-04
  • 2013-08-04
  • 2017-09-21
  • 2015-03-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多