zoukankan      html  css  js  c++  java
  • 常用工具类

    解析查询字符串

            /// <summary>
            /// 解析查询字符串
            /// </summary>
            private NameValueCollection GetQueryString(string queryString, Encoding encoding, bool isEncoded)
            {
                var result = new NameValueCollection(StringComparer.OrdinalIgnoreCase);
                if (!string.IsNullOrEmpty(queryString))
                {
                    int count = queryString.Length;
                    for (int i = 0; i < count; i++)
                    {
                        int startIndex = i;
                        int index = -1;
                        while (i < count)
                        {
                            char item = queryString[i];
                            if (item == '=')
                            {
                                if (index < 0)
                                {
                                    index = i;
                                }
                            }
                            else if (item == '&')
                            {
                                break;
                            }
                            i++;
                        }
                        string key = null;
                        string value = null;
                        if (index >= 0)
                        {
                            key = queryString.Substring(startIndex, index - startIndex);
                            value = queryString.Substring(index + 1, (i - index) - 1);
                        }
                        else
                        {
                            key = queryString.Substring(startIndex, i - startIndex);
                        }
                        if (isEncoded)
                        {
                            result[HttpUtility.UrlDecode(key, encoding)] = HttpUtility.UrlDecode(value, encoding);
                        }
                        else
                        {
                            result[key] = value;
                        }
                        if ((i == (count - 1)) && (queryString[i] == '&'))
                        {
                            result[key] = string.Empty;
                        }
                    }
                }
                return result;
            }
    

    解析Key=Value,字符串

            /// <summary>
            /// 解析Key=Value,字符串
            /// </summary>
            private NameValueCollection GetContentString(string queryString)
            {
                var result = new NameValueCollection(StringComparer.OrdinalIgnoreCase);
                if (string.IsNullOrEmpty(queryString))
                    return result;
                int count = queryString.Length;
                for (int i = 0; i < count; i++)
                {
                    int startIndex = i;
                    int index = -1;
                    while (i < count)
                    {
                        char item = queryString[i];
                        if (item == '=')
                        {
                            if (index < 0)
                            {
                                index = i;
                            }
                        }
                        else if (item == ',')
                        {
                            break;
                        }
                        i++;
                    }
                    string key = null;
                    string value = null;
                    if (index >= 0)
                    {
                        key = queryString.Substring(startIndex, index - startIndex).Trim();
                        value = queryString.Substring(index + 1, (i - index) - 1);
                    }
                    else
                    {
                        key = queryString.Substring(startIndex, i - startIndex).Trim();
                    }
                    result[key] = value;
                    if ((i == (count - 1)) && (queryString[i] == ','))
                    {
                        result[key] = string.Empty;
                    }
                }
                return result;
            }
    
  • 相关阅读:
    Powerdesigner SqlServer转Oracle(转)
    ASP.NET jquery.uploadify上传控件中文乱码解决办法(转)
    网页上显示数学公式目前哪种方案最好? 来自知乎
    sql server 自增长id 允许插入显示值
    (转)【深入浅出jQuery】源码浅析2--奇技淫巧
    (转)js activexobject调用客户机exe文件
    搞笑代码注释,佛祖保佑 永无BUG
    json 递归查找某个节点
    c# string.format json字符串 formatException错误
    验证list的底层数据结构
  • 原文地址:https://www.cnblogs.com/rsls/p/13038704.html
Copyright © 2011-2022 走看看