Split out ints from string

前端 未结 13 1470
情歌与酒
情歌与酒 2021-01-17 17:45

Let\'s say I have a web page that currently accepts a single ID value via a url parameter:
http://example.com/mypage.aspx?ID=1234

I want to change it to acce

13条回答
  •  萌比男神i
    2021-01-17 18:01

    List convertIDs = new List;
    string[] splitIds = ids.split(',');
    foreach(string s in splitIds)
    {
        convertIDs.Add(int.Parse(s));
    }
    

    For completeness you will want to put try/catches around the for loop (or around the int.Parse() call) and handle the error based on your requirements. You can also do a tryparse() like so:

    List convertIDs = new List;
    string[] splitIds = ids.split(',');
    foreach(string s in splitIds)
    {
        int i;
        int.TryParse(out i);
        if (i != 0)
           convertIDs.Add(i);
    }
    

提交回复
热议问题