LoginSignup
5
2

More than 5 years have passed since last update.

カンマ区切りの文字列を分割してデータ型を変換する

Last updated at Posted at 2017-02-03

文字列から配列またはListコレクションにデータを取得します。
その時にString型からint型にデータ型を変更します。

取り出し元の変数
string str = "1,2,3,4,5";
配列を取得する例(Select使用)
int[] result  = str
.Split(',')
.Select(a => int.Parse(a))
.ToArray();
  • List形式のデータを取得
List型を取得する例(ConvertAll使用)
List<int> result = str
.Split(',')
.ToList()
.ConvertAll(a => int.Parse(a));

または

List型を取得する例(Select使用)
List<int> result = str
.Split(',')
.Select(a => int.Parse(a))
.ToList();
5
2
0

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
5
2