Split out ints from string

前端 未结 13 1347
情歌与酒
情歌与酒 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:13

    No offense to those who provided clear answers, but many people seem to be answering your question instead of addressing your problem. You want multiple IDs, so you think you could this this:

    http://example.com/mypage.aspx?IDs=1234,4321,6789

    The problem is that this is a non-robust solution. In the future, if you want multiple values, what do you do if they have commas? A better solution (and this is perfectly valid in a query string), is to use multiple parameters with the same name:

    http://example.com/mypage.aspx?ID=1234;ID=4321;ID=6789

    Then, whatever query string parser you use should be able to return a list of IDs. If it can't handle this (and also handle semi-colons instead of ampersands), then it's broken.

    0 讨论(0)
  • 2021-01-17 18:15

    If you like the functional style, you can try something like

        string ids = "1,2,3,4,5";
    
        List<int> l = new List<int>(Array.ConvertAll(
            ids.Split(','), new Converter<string, int>(int.Parse)));
    

    No lambdas, but you do have Converters and Predicates and other nice things that can be made from methods.

    0 讨论(0)
  • 2021-01-17 18:22

    I see my answer came rather late, i.e. several other had written the same. Therefore I present an alternative method using regular expressions to validate and divide the string.

    class Program
    {
        //Accepts one or more groups of one or more digits, separated by commas.
        private static readonly Regex CSStringPattern = new Regex(@"^(\d+,?)*\d+$");
    
        //A single ID inside the string. Must only be used after validation
        private static readonly Regex SingleIdPattern = new Regex(@"\d+");
    
        static void Main(string[] args)
        {
            string queryString = "1234,4321,6789";
    
            int[] ids = ConvertCommaSeparatedStringToIntArray(queryString);
        }
    
        private static int[] ConvertCommaSeparatedStringToIntArray(string csString)
        {
            if (!CSStringPattern.IsMatch(csString))
                throw new FormatException(string.Format("Invalid comma separated string '{0}'",
                                                        csString));
    
            List<int> ids = new List<int>();
            foreach (Match match in SingleIdPattern.Matches(csString))
            {
                ids.Add(int.Parse(match.Value)); //No need to TryParse since string has been validated
            }
            return ids.ToArray();
        }
    }
    
    0 讨论(0)
  • 2021-01-17 18:22

    You'll just have to foreach through them and int.TryParse each one of them. after that just add to the list.

    Nevermind - @Splash beat me to it

    0 讨论(0)
  • 2021-01-17 18:26

    All I can think of is to loop over the list of strings (which you have got from performing a split) and doing something like int.TryParse() on them one after the other and putting them into a new List<int>. Encapsulate it in a nice little helper method somewhere and it won't be too horrid.

    0 讨论(0)
  • 2021-01-17 18:28

    Something like this might work:

    public static IList<int> GetIdListFromString(string idList)
    {
        string[] values = idList.Split(',');
    
        List<int> ids = new List<int>(values.Length);
    
        foreach (string s in values)
        {
            int i;
    
            if (int.TryParse(s, out i))
            {
                ids.Add(i);
            }
        }
    
        return ids;
    }
    

    Which would then be used:

    string intString = "1234,4321,6789";
    
    IList<int> list = GetIdListFromString(intString);
    
    foreach (int i in list)
    {
        Console.WriteLine(i);
    }
    
    0 讨论(0)
提交回复
热议问题