1. 程式人生 > >Asp.net/java 的 [字串Url] 引數處理

Asp.net/java 的 [字串Url] 引數處理

    CSDN廣告是越來越多了,所有部落格筆記不再更新,新網址 DotNet筆記

先看一個msdn  Uri類:Uri類       和本篇文章沒關係,只是為了方便查詢

        /// <summary>
        /// 獲取一個url中字串中?後面的值,以Dictionary<key, value>形式返回
        /// </summary>
        /// <param name="url">url</param>
        /// <returns>Dictionary<key, value>形式返回</returns>
        public static Dictionary<string, string> GetParams(string url)
        {
            int intPos = url.IndexOf("?");
            if (url == null || url.Length < 1 || intPos < 1)
            {
                return null;
            }
            else
            {
                Dictionary<string, string> dt = new Dictionary<string, string>();
                string strRight = url.Substring(intPos + 1);
                string[] arrPram = StringSplit(strRight, "&");
                for (int i = 0; i < arrPram.Length; i++)
                {
                    string[] arrPramName = StringSplit(arrPram[i], "=");
                    dt.Add(arrPramName[0].ToLower(), arrPramName[1].ToLower());
                }
                return dt;
            }
        }


        /// <summary>
        /// 用指定的key:value  替換目標url的引數是叫key的值,如果不存在,就新增一個引數key=value
        /// </summary>
        /// <param name="url">目標url</param>
        /// <param name="key">目標引數key名字</param>
        /// <param name="value">新的Value</param>
        /// <returns>替換後新的url</returns>
        public static string ReplaceUrlParam(string url, string key, string value)
        {
            string new_returnurl = "";
            Dictionary<string, string> d = Safe.GetParams(url.ToLower());
            d = d ?? new Dictionary<string, string>();
            if (d.ContainsKey(key))
            {
                d.Remove(key);
                d.Add(key, value);
            }
            else
            {
                d.Add(key, value);
            }
            if (d != null && d.Count > 0)
            {
                int i = 0;
                foreach (KeyValuePair<string, string> kv in d)
                {
                    if (i == 0)
                    {
                        new_returnurl += "?" + kv.Key + "=" + kv.Value;
                    }
                    else
                    {
                        new_returnurl += "&" + kv.Key + "=" + kv.Value;
                    }
                    i++;
                }
            }
            new_returnurl = url.Split('?')[0] + new_returnurl;
            return new_returnurl;
        }

java: 

/**
	 * 拆分URL,返回引數的鍵值對
	 * @Method: GetParams 
	 * @ @param url 被處理url
	 * @ @return
	 * @return Map<String,String>
	 * @throws
	 */
	public static Map<String, String> GetParams(String url) {
		int intPos = url.indexOf("?");
		if (url == null || url.length() < 1 || intPos < 1) {
			return null;
		} else {
			Map<String, String> map = new HashMap<String, String>();
			String strRight = url.substring(intPos + 1);
			String[] arrPram = strRight.split("&");

			for (int i = 0; i < arrPram.length; i++) {
				String[] arrPramName = arrPram[i].split("=");
				map.put(arrPramName[0].toLowerCase(), arrPramName[1].toLowerCase());
			}
			return map;
		}
	}


	public static String ReplaceUrlParam(String url, String key, String value) {
		String new_returnurl = "";
		Map<String, String> map = GetParams(url);
		if (map == null) {
			map = new HashMap<String, String>();
		}
		if (map.containsKey(key)) {
			map.remove(key);
			if(value==null||value==""){
			}else{
				map.put(key, value);
			}
		}else{
			if(value==null||value==""){
			}else{
				map.put(key, value);
			}
		}
		
		if (map != null && map.size() > 0) {
			int i = 0;
			for (Map.Entry<String, String> kv : map.entrySet()) {

				if (i == 0) {
					new_returnurl += "?" + kv.getKey() + "=" + kv.getValue();
				} else {
					new_returnurl += "&" + kv.getKey() + "=" + kv.getValue();
				}
				i++;
			}
		}
		new_returnurl = url.split("[?]")[0] + new_returnurl;
		return new_returnurl;
	}

注意:java版本的ReplaceUrlParam比C#版本的多個去除引數功能(如果value值為null,那麼,url就會去掉Key對應的引數)