Split out ints from string

前端 未结 13 1483
情歌与酒
情歌与酒 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条回答
  •  灰色年华
    2021-01-17 18:03

    Final code snippet that takes what I hope is the best from all the suggestions:

    Function GetIDs(ByVal IDList As String) As List(Of Integer)
        Dim SplitIDs() As String = IDList.Split(new Char() {","c}, StringSplitOptions.RemoveEmptyEntries)
        GetIDs = new List(Of Integer)(SplitIDs.Length)
        Dim CurID As Integer
        For Each id As String In SplitIDs
            If Integer.TryParse(id, CurID) Then GetIDs.Add(CurID)
        Next id
    End Function
    

    I was hoping to be able to do it in one or two lines of code inline. One line to create the string array and hopefully find something in the framework I didn't already know to handle importing it to a List that could handle the cast intelligently. But if I must move it to a method then I will. And yes, I'm using VB. I just prefer C# for asking questions because they'll get a larger audience and I'm just about as fluent.

提交回复
热议问题