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;
            }
    
  • 相关阅读:
    uva 1637 Double Patience
    Emacs ^ Vim
    uva 11181 Probability|Given
    uva 10491 Cows and Cars
    uva 1636 Headshot
    hdu 4336 Card Collector
    zoj 3640 Help Me Escape
    Codeforces 148 D Bag of mice
    hdu 4405 Aeroplane chess
    hdu 3853 LOOPS
  • 原文地址:https://www.cnblogs.com/rsls/p/13038704.html
Copyright © 2011-2022 走看看