码迷,mamicode.com
首页 > Web开发 > 详细

sage开发url替换字符串

时间:2017-06-20 22:32:20      阅读:205      评论:0      收藏:0      [点我收藏+]

标签:tostring   区分   ati   art   text   rem   eps   idv   switch   

        /// <summary>
        /// Url字段值替换,无该字段则加入
        /// </summary>
        /// <param name="strRepSource">须要改动的源URL</param>
        /// <param name="strRepField">字段,注意区分大写和小写。如:ID或Key37</param>
        /// <param name="strNewValue">新值</param>
        /// <returns>替换后新Url</returns>
        public static string UrlReplace(string strRepSource, string strRepField, string strNewValue)
        {
            System.Text.StringBuilder strBuilder = new System.Text.StringBuilder();
            bool bSwitch = false;

            if (strRepSource.Contains("&" + strRepField + "="))
            {
                strRepField = "&" + strRepField + "=";
                bSwitch = true;
            }
            else if (strRepSource.Contains("?" + strRepField + "="))
            {
                strRepField = "?" + strRepField + "=";
                bSwitch = true;
            }

            if (bSwitch)
            {
                string[] strArray = strRepSource.Split(new string[] { strRepField }, 2, StringSplitOptions.RemoveEmptyEntries);
                strBuilder.Append(strArray[0]);
                strBuilder.Append(strRepField);
                strBuilder.Append(strNewValue);

                if (strArray.Length == 2 && strArray[1].Contains("&"))
                {
                    string[] strAy = strArray[1].Split(new char[] { ‘&‘ }, 2, StringSplitOptions.RemoveEmptyEntries);
                    strBuilder.Append("&" + strAy[strAy.Length - 1]);
                }
            }
            else
            {
                strBuilder.Append(strRepSource);
                strBuilder.Append(‘&‘);
                strBuilder.Append(strRepField);
                strBuilder.Append(‘=‘);
                strBuilder.Append(strNewValue);
            }

            return strBuilder.ToString();
        }

        /// <summary>
        /// Url字段值替换。无该字段则加入
        /// </summary>
        /// <param name="strRepSource">须要改动的源URL</param>
        /// <param name="strRepField">字段,注意区分大写和小写。如:ID或Key37</param>
        /// <param name="iNewValue">新值</param>
        /// <returns>替换后新Url</returns>
        public static string UrlReplace(string strRepSource, string strRepField, int iNewValue)
        {
            return UrlReplace(strRepSource, strRepField, iNewValue.ToString());
        }


        /// <summary>
        /// Url构造函数
        /// </summary>
        /// <param name="strUrl">原Url</param>
        /// <param name="strIdField">实体ID字段</param>
        /// <param name="iIdValue">ID值</param>
        /// <param name="strJvalue">选择值。实现相应选择卡凹陷</param>
        /// <returns>构造后的Url,包含Key37和Key58值的加入</returns>
        public static string UrlStructure(string strUrl, string strIdField, string iIdValue, string strJvalue)
        {
            strUrl = UrlReplace(strUrl, strIdField, iIdValue);
            strUrl = UrlReplace(strUrl, "Key37", iIdValue);
            strUrl = UrlReplace(strUrl, "Key58", iIdValue);
            if (!string.IsNullOrEmpty(strJvalue))
            {
                strUrl = UrlReplace(strUrl, "J", strJvalue);
            }
            return strUrl;
        }

        /// <summary>
        /// 加入參数
        /// </summary>
        public static string AddParameters(string url, string k, string v)
        {
            if (!string.IsNullOrEmpty(k))
            {
                url = UrlReplace(url, k, v);
            }
            return url;
        }

        /// <summary>
        /// Url构造函数
        /// </summary>
        /// <param name="strUrl">原Url</param>
        /// <param name="strIdField">实体ID字段</param>
        /// <param name="iIdValue">ID值</param>
        /// <returns>构造后的Url。包含Key37和Key58值的加入</returns>
        public static string UrlStructure(string strUrl, string strIdField, int iIdValue)
        {
            strUrl = UrlStructure(strUrl, strIdField, iIdValue.ToString(), string.Empty);
            return strUrl;
        }

        /// <summary>
        /// Url构造函数
        /// </summary>
        /// <param name="strUrl">原Url</param>
        /// <param name="strIdField">实体ID字段</param>
        /// <param name="iIdValue">ID值</param>
        /// <returns>构造后的Url。包含Key37和Key58值的加入</returns>
        public static string UrlStructure(string strUrl, string strIdField, string iIdValue)
        {
            strUrl = UrlStructure(strUrl, strIdField, iIdValue, string.Empty);
            return strUrl;
        }

sage开发url替换字符串

标签:tostring   区分   ati   art   text   rem   eps   idv   switch   

原文地址:http://www.cnblogs.com/yangykaifa/p/7056283.html

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