C#字符串操作

时间:2022-07-13 16:13:17
/// <summary>
/// 返回上一个页面的地址
/// </summary>
/// <returns>上一个页面的地址</returns>
public static string GetUrlReferrer()
{
string retVal = null;

try
{
retVal = HttpContext.Current.Request.UrlReferrer.ToString();
}
catch{}

if (retVal == null)
return "";

return retVal;
}
去除HTML代码
C#字符串操作C#字符串操作
/// <summary>
        /// 去除HTML代码
        /// </summary>
        /// <param name="Str"></param>
        /// <returns></returns>
        public static string LostHTML(string Str)
        {
            string Re_STR = "";
            if (Str != null)
            {
                if (Str != string.Empty)
                {
                    string pattern = "<\v*[^<>]*>";
                    Re_STR = Regex.Replace(Str, pattern, "");

                }
            }

            return (Re_STR.Replace("\\r\\n", "")).Replace("\\r", "");
        }
View Code
手机号码中间4位替换成 ****
C#字符串操作C#字符串操作
       #region 手机号码中间4位替换成 ****
        public static string rpMobile(string str)
        {
            if (string.IsNullOrEmpty(str))
            {
                return "";
            }
            str = str.Replace(str.Substring(3, 4), "****");
            return str;
        }
        #endregion
View Code

身份证中间10位替换成 ****

C#字符串操作C#字符串操作
  #region 身份证中间10位替换成 ****
        public static string rpIDCARD(string str)
        {
            if (string.IsNullOrEmpty(str))
            {
                return "";
            }
            if (str.Length == 18)
            {
                str = str.Replace(str.Substring(4, 10), "********");
            }
            else if (str.Length == 16)
            {
                str = str.Replace(str.Substring(4, 8), "********");
            }
            else
            {

            }
            return str;
        }
        #endregion
View Code

截取字符长度

C#字符串操作C#字符串操作
     #region 截取字符长度
        /// <summary>
        /// 截取字符长度
        /// </summary>
        /// <param name="inputString">字符</param>
        /// <param name="len">长度</param>
        /// <returns></returns>
        public static string CutString(string inputString, int len)
        {
            if (string.IsNullOrEmpty(inputString))
                return "";
            inputString = DropHTML(inputString);
            ASCIIEncoding ascii = new ASCIIEncoding();
            int tempLen = 0;
            string tempString = "";
            byte[] s = ascii.GetBytes(inputString);
            for (int i = 0; i < s.Length; i++)
            {
                if ((int)s[i] == 63)
                {
                    tempLen += 2;
                }
                else
                {
                    tempLen += 1;
                }

                try
                {
                    tempString += inputString.Substring(i, 1);
                }
                catch
                {
                    break;
                }

                if (tempLen > len)
                    break;
            }
            //如果截过则加上半个省略号 
            byte[] mybyte = System.Text.Encoding.Default.GetBytes(inputString);
            if (mybyte.Length > len)
                tempString += "";
            return tempString;
        }
        #endregion
View Code