常用工具类

Posted rsls

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了常用工具类相关的知识,希望对你有一定的参考价值。

解析查询字符串

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

以上是关于常用工具类的主要内容,如果未能解决你的问题,请参考以下文章

如何通过单击片段内的线性布局从片段类开始新活动?下面是我的代码,但这不起作用

如何在片段中使用按钮[关闭]

微信小程序代码片段

常用python日期日志获取内容循环的代码片段

C#常用代码片段备忘

PHP代码-psysh调试代码片段工具