zoukankan      html  css  js  c++  java
  • 类似aaa?a=1&b=2&c=3&d=4,如何将问号以后的数据变为键值对

    string result = "aaa?a=1&b=2&c=3&d=4";
    string[] array = result.Split('?');
    //string a = System.Web.HttpUtility.ParseQueryString(array[1]).Get("a");
    System.Collections.Specialized.NameValueCollection abcd = System.Web.HttpUtility.ParseQueryString(array[1]);
    string a = abcd.Get("a");
    string b = abcd.Get("b");
    string c = abcd.Get("c");
    string d = abcd.Get("d");

    ParseQueryString的内部实现(字符串截取):

     int num = (s != null) ? s.Length : 0;
     System.Collections.Specialized.NameValueCollection a = new System.Collections.Specialized.NameValueCollection(); 
                for (int i = 0; i < num; i++)
                {
                    int startIndex = i;
                    int num4 = -1;
                    while (i < num)
                    {
                        char ch = s[i];
                        if (ch == '=')
                        {
                            if (num4 < 0)
                            {
                                num4 = i;
                            }
                        }
                        else if (ch == '&')
                        {
                            break;
                        }
                        i++;
                    }
                    string str = null;
                    string str2 = null;
                    if (num4 >= 0)
                    {
                        str = s.Substring(startIndex, num4 - startIndex);
                        str2 = s.Substring(num4 + 1, (i - num4) - 1);
                    }
                    else
                    {
                        str2 = s.Substring(startIndex, i - startIndex);
                    }
    
                    a.Add(str, str2);
                   
                    if ((i == (num - 1)) && (s[i] == '&'))
                    {
                        a.Add(null, string.Empty);
                    }
                }

    点击链接学习:膜拜高手

  • 相关阅读:
    桥梁模式
    原型模式
    css backgroundposition
    eclipse配置了maven,项目报错
    SQL 练习题目
    Springmvc + Ibatis 搭建 点餐系统
    Delphi 性能优化工具
    Delphi的接口陷阱
    delphi 内存管理,定期释放
    Delphi制作数据感知控件之浮想联翩
  • 原文地址:https://www.cnblogs.com/AlienXu/p/6875985.html
Copyright © 2011-2022 走看看