今后可能常用的一些正则表达式

1。

        /// <summary>
        /// 去除html和空格(&nbsp;)
        /// </summary>
        /// <param name="content"></param>
        /// <returns></returns>
        public static string DelHtmlAndSpace(string content)
        {
            return Regex.Replace(content, @"<(.[^>]*)>|(/s|(&nbsp;))", "", RegexOptions.IgnoreCase); ;
        }

 

2。   

        /// <summary>
        /// 将特殊字符转换成转义符[适用于评论]
        /// </summary>
        /// <param name="content"></param>
        /// <returns></returns>
        public static string ReplaceSpecialMarkToCode(string content)
        {
            #region

            return content.Replace("<", "&lt;").Replace(">", "&gt;");

            #endregion
        }

3。   

        此方法还有许多改进的地方

        /// <summary>
        /// 截取字符串
        /// </summary>
        /// <param name="str">需要截取的字符串</param>
        /// <param name="length">截取的长度</param>
        /// <returns></returns>
        public static string GetSubString(string str, int length)
        {
            string temp =str;
            double j = 0;
            int k = 0;
            for (int i = 0; i < temp.Length; i++)
            {
                if (Regex.IsMatch(temp.Substring(i, 1), @"[/u4e00-/u9fa5|/u3001-/u3002|/uff1f|/uff01|/uff0c|/uff1a-/uff1b|/u2018-/u2019|/u201c-/u201d|/u3014-/u3015|/u3010-/u3011|/u2014|/u3008-/u3009]+"))
                //汉字和中文标点符号(、。、?、!、,、:;、‘’、“”、()、【】、—、《》)
                {
                    j += 2;
                }
                else if (Regex.IsMatch(temp.Substring(i, 1), @"[/u0041-/u005a]+"))//大写字母ABC..
                {
                    j += 4 / 3.0;
                }
                else
                {
                    j += 1;
                }
                if (j <= length)
                {
                    k += 1;
                }
                if (j >= length)
                {
                    return temp.Substring(0, k) + "...";
                }
            }
            return temp;
        }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值