Array提供了copy方法,Copy方法有四个重载

  • 使用格式:
  • Array.Copy(数组1,数组2,长度);
  • Array.Copy(数组1,数组2,Int64);
  • Array.Copy(数组1,指定索引,数组2,指定索引,长度);
  • Array.Copy(数组1,指定索引,数组2,指定索引,Int64);

    合并拆分数组

    1. /// <summary>
    2. /// 合并数组
    3. /// </summary>
    4. /// <param name="First">第一个数组</param>
    5. /// <param name="Second">第二个数组</param>
    6. /// <returns>合并后的数组(第一个数组+第二个数组,长度为两个数组的长度)</returns>
    7. public string[] MergerArray(string[] First, string[] Second)
    8. {
    9. string[] result = new string[First.Length + Second.Length];
    10. First.CopyTo(result, 0);
    11. Second.CopyTo(result, First.Length);
    12. return result;
    13. }
    14. /// <summary>
    15. /// 数组追加
    16. /// </summary>
    17. /// <param name="Source">原数组</param>
    18. /// <param name="str">字符串</param>
    19. /// <returns>合并后的数组(数组+字符串)</returns>
    20. public string[] MergerArray(string[] Source, string str)
    21. {
    22. string[] result = new string[Source.Length + 1];
    23. Source.CopyTo(result, 0);
    24. result[Source.Length] = str;
    25. return result;
    26. }
    27. /// <summary>
    28. /// 从数组中截取一部分成新的数组
    29. /// </summary>
    30. /// <param name="Source">原数组</param>
    31. /// <param name="StartIndex">原数组的起始位置</param>
    32. /// <param name="EndIndex">原数组的截止位置</param>
    33. /// <returns></returns>
    34. public string[] SplitArray(string[] Source, int StartIndex, int EndIndex)
    35. {
    36. try
    37. {
    38. string[] result = new string[EndIndex - StartIndex+1];
    39. for (int i = 0; i <= EndIndex - StartIndex; i++) result[i] = Source[i+StartIndex];
    40. return result;
    41. }
    42. catch (IndexOutOfRangeException ex)
    43. {
    44. throw new Exception(ex.Message);
    45. }
    46. }
    47. /// <summary>
    48. /// 不足长度的前面补空格,超出长度的前面部分去掉
    49. /// </summary>
    50. /// <param name="First">要处理的数组</param>
    51. /// <param name="byteLen">数组长度</param>
    52. /// <returns></returns>
    53. public string[] MergerArray(string[] First, int byteLen)
    54. {
    55. string[] result;
    56. if (First.Length > byteLen)
    57. {
    58. result = new string[byteLen];
    59. for (int i = 0; i < byteLen; i++) result[i] = First[i + First.Length - byteLen];
    60. return result;
    61. }
    62. else
    63. {
    64. result = new string[byteLen];
    65. for (int i = 0; i < byteLen; i++) result[i] = " ";
    66. First.CopyTo(result, byteLen-First.Length);
    67. return result;
    68. }
    69. }

    调用

    1. string[] student1 = { "a", "b", "c", "d", "e", "f"};
    2. string[] student2 = { "0", "1", "2", "3", "4", "5", "6", "6", "1", "8", "16", "10", "45", "37", "82" };
    3. string student3 = "Test";
    4. //两个数组相加 结果--> { "a", "b", "c", "d", "e", "f","0", "1", "2", "3", "4", "5", "6", "6", "1", "8", "16", "10", "45", "37", "82" };
    5. string[] Merger1 = MergerArray(student1, student2);
    6. //数组+字符串 结果-->{ "a", "b", "c", "d", "e", "f","Test"};
    7. string[] Merger2 = MergerArray(student1, student3);
    8. //不足10位的,前面补空格 结果-->{" ", " ", " ", " ", "a", "b", "c", "d", "e", "f"};
    9. string[] Merger3 = MergerArray(student1, 10);
    10. //超过3位的,只取最后3后 结果-->{"d", "e", "f"};
    11. string[] Merger4 = MergerArray(student1, 3);
    12. //截取部分数组 结果-->{"c", "d", "e"};
    13. string[] SplitArray1 = SplitArray(student1, 2, 4);