Split string, convert ToList() in one line

后端 未结 10 1926
借酒劲吻你
借酒劲吻你 2020-11-27 10:00

I have a string that has numbers

string sNumbers = \"1,2,3,4,5\";

I can split it then convert it to List

         


        
10条回答
  •  南笙
    南笙 (楼主)
    2020-11-27 10:37

    Better use int.TryParse to avoid exceptions;

    var numbers = sNumbers
                .Split(',')
                .Where(x => int.TryParse(x, out _))
                .Select(int.Parse)
                .ToList();
    

提交回复
热议问题