gpt4 book ai didi

C# 字符串数组只获取前 10 个值

转载 作者:可可西里 更新时间:2023-11-01 08:51:04 26 4
gpt4 key购买 nike

我有一个字符串数组,它有一个像这样的值列表

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

我只想获取前十个,所以我的输出看起来像这样并将其存储在另一个字符串数组中。

1
2
3
4
5
6
7
8
9
10

看起来很简单,我就是想不出来

最佳答案

for (int i=0; i<Math.Min(10, array.Length); i++)
Console.WriteLine(array[i]);

foreach (int i in array.Take(10))        
Console.WriteLine(array[i]);

编辑:根据您的评论,您希望它在字符串数组中。这是你必须做的

string[] numbers = array.Take(10).Select(i=>i.ToString()).ToArray();

关于C# 字符串数组只获取前 10 个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7858185/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com