gpt4 book ai didi

c# - 将数字字符串转换为数字数组 C#?

转载 作者:行者123 更新时间:2023-12-03 19:25:30 25 4
gpt4 key购买 nike

我有一个由 9 个空格分隔的整数组成的字符串,例如 "3 4 6 9 8 8 2 3 4",我想将其转换为 3x3 int 数组。

一个简单的解决方案是对新矩阵进行两次循环并转换字符串值就我而言。有没有更优雅的方法来做到这一点?

最佳答案

使用我的分割扩展 Split a collection into `n` parts with LINQ?

var nums = s.Split(' ').Select(n=>Int32.Parse(n)).ToList();
var grid = nums.Split(nums.Count / 3);

关于c# - 将数字字符串转换为数字数组 C#?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4553706/

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