I have a string array that has a list of values like this
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
I am trying only to get the first ten so my output looks like this and store it another string array.
1
2
3
4
5
6
7
8
9
10
it seems really easy i just can't figure it out
for (int i=0; i<Math.Min(10, array.Length); i++)
Console.WriteLine(array[i]);
OR
foreach (int i in array.Take(10))
Console.WriteLine(array[i]);
EDIT: Based on your comment that you want it in a string array. Here is what you have to do
string[] numbers = array.Take(10).Select(i=>i.ToString()).ToArray();
You can use Linq. You need to include the reference and the using directive:
using System.Linq;
theStringsArray.Take(10).ToArray();
您可以使用
Array.Copy(SourceArray, DestinationArray, 10);
如果你真的决定你根本不关心源数组,你可以简单地调整它的大小以摆脱不需要的值:
Array.Resize(ref myArray, 10);
You can use an ArraySegment to do this without creating intermediate lists or enumerators:
using System;
string[] newArray = (new ArraySegment<string>(oldArray, 0, 10)).ToArray();
This can also be used to take any part of the array.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.