C#中的String []为int [] [英] C# string[] to int[]

查看:71
本文介绍了C#中的String []为int []的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法转换成字符串数组为int数组那么容易,因为在C#中解析字符串为int。

Is there a way to convert string arrays to int arrays as easy as parsing an string to an int in C#.

int a = int.Parse("123");
int[] a = int.Parse("123,456".Split(’,’)); // this don't work.

我一直在使用扩展​​方法为int类添加这个功能我自己试过,但他们不会成为静态的。

I have tried using extension methods for the int class to add this functionality myself but they don't become static.

这是如何做到这一点的快速和漂亮的任何想法?

Any idea on how to do this fast and nice?

推荐答案

这LINQ查询应该这样做:

This linq query should do it:

strArray.Select(s => int.Parse(s)).ToArray()

这篇关于C#中的String []为int []的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆