sage开发url替换字符串

2021-07-20 10:04

阅读:419

        ///


        /// Url字段值替换,无该字段则加入
        ///

        /// 须要改动的源URL
        /// 字段,注意区分大写和小写。如:ID或Key37
        /// 新值
        /// 替换后新Url
        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();
        }

        ///


        /// Url字段值替换。无该字段则加入
        ///

        /// 须要改动的源URL
        /// 字段,注意区分大写和小写。如:ID或Key37
        /// 新值
        /// 替换后新Url
        public static string UrlReplace(string strRepSource, string strRepField, int iNewValue)
        {
            return UrlReplace(strRepSource, strRepField, iNewValue.ToString());
        }


        ///


        /// Url构造函数
        ///

        /// 原Url
        /// 实体ID字段
        /// ID值
        /// 选择值。实现相应选择卡凹陷
        /// 构造后的Url,包含Key37和Key58值的加入
        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;
        }

        ///


        /// 加入參数
        ///

        public static string AddParameters(string url, string k, string v)
        {
            if (!string.IsNullOrEmpty(k))
            {
                url = UrlReplace(url, k, v);
            }
            return url;
        }

        ///


        /// Url构造函数
        ///

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

        ///


        /// Url构造函数
        ///

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


评论


亲,登录后才可以留言!