6
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

string配列をint配列に変換

Last updated at Posted at 2020-04-14

string配列をint配列へできるだけスッキリと記述したかったので
ググった結果をメモメモ(~_~メ)

string[] strArray = { "1", "10", "100", "1000", "10000" };
int[] intArray = new int[strArray.Length];
for (int i = 0; i < intArray.Length; i++)
  intArray[i] = int.Parse(strArray[i]);

上記をスッキリと。。。

LINQ版
int[] intArray = strArray
  .Select(int.Parse)
  .ToArray();
Array.ConvertAll()版
int[] intArray = Array.ConvertAll(strArray, int.Parse);
Listとforeachを利用
var intList = new List<int>(strArray.Length);
foreach(var s in strArray)
    intList.Add(int.Parse(s));
var intArray = intList.ToArray();

もっといい書き方があればぜひコメントへお願いいたします(^_^

6
4
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?