码迷,mamicode.com
首页 > 其他好文 > 详细

常用工具类

时间:2020-06-03 17:32:24      阅读:51      评论:0      收藏:0      [点我收藏+]

标签:coding   i++   substring   cas   encoding   index   ali   query   http   

解析查询字符串

        /// <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;
        }

常用工具类

标签:coding   i++   substring   cas   encoding   index   ali   query   http   

原文地址:https://www.cnblogs.com/rsls/p/13038704.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!