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

sage开发url替换字符串

时间:2014-07-15 10:25:38      阅读:230      评论:0      收藏:0      [点我收藏+]

标签:sage开发url替换字符串

        /// <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替换字符串,布布扣,bubuko.com

sage开发url替换字符串

标签:sage开发url替换字符串

原文地址:http://blog.csdn.net/y_f123/article/details/37808639

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