截取字符串

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;


namespace PKE.Global
{
    public class GeneralHelp
    {
        /// <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;
        }


        /// <summary>
        /// 截取字符串不带“...”
        /// </summary>
        /// <param name="str">需要截取的字符串</param>
        /// <param name="length">截取的长度</param>
        /// <returns></returns>
        public static string GetSubStringPrue(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]+"))
                {
                    j += 4 / 3.0;
                }
                else
                {
                    j += 1;
                }
                if (j <= length)
                {
                    k += 1;
                }
                if (j >= length)
                {
                    return temp.Substring(0, k);
                }
            }
            return temp;
        }


        /// <summary>
        /// 去除HTML标签
        /// </summary>
        /// <param name="htmlstring"></param>
        /// <returns></returns>
        public static string DelHtml(string htmlstring)
        {
            #region
            //删除脚本


            htmlstring = Regex.Replace(htmlstring, @"<script[^>]*?>.*?</script>", "", RegexOptions.IgnoreCase);


            //删除HTML


            htmlstring = Regex.Replace(htmlstring, @"<(.[^>]*)>", "", RegexOptions.IgnoreCase);


            htmlstring = Regex.Replace(htmlstring, @"([\r\n])[\s]+", "", RegexOptions.IgnoreCase);


            htmlstring = Regex.Replace(htmlstring, @"-->", "", RegexOptions.IgnoreCase);


            htmlstring = Regex.Replace(htmlstring, @"<!--.*", "", RegexOptions.IgnoreCase);


            htmlstring = Regex.Replace(htmlstring, @"&(quot|#34);", "\"", RegexOptions.IgnoreCase);


            htmlstring = Regex.Replace(htmlstring, @"&(amp|#38);", "&", RegexOptions.IgnoreCase);


            htmlstring = Regex.Replace(htmlstring, @"&(lt|#60);", "<", RegexOptions.IgnoreCase);


            htmlstring = Regex.Replace(htmlstring, @"&(gt|#62);", ">", RegexOptions.IgnoreCase);


            htmlstring = Regex.Replace(htmlstring, @"&(nbsp|#160);", " ", RegexOptions.IgnoreCase);


            htmlstring = Regex.Replace(htmlstring, @"&(iexcl|#161);", "\xa1", RegexOptions.IgnoreCase);


            htmlstring = Regex.Replace(htmlstring, @"&(cent|#162);", "\xa2", RegexOptions.IgnoreCase);


            htmlstring = Regex.Replace(htmlstring, @"&(pound|#163);", "\xa3", RegexOptions.IgnoreCase);


            htmlstring = Regex.Replace(htmlstring, @"&(copy|#169);", "\xa9", RegexOptions.IgnoreCase);


            htmlstring = Regex.Replace(htmlstring, @"&#(\d+);", "", RegexOptions.IgnoreCase);


            htmlstring.Replace("<", "");


            htmlstring.Replace(">", "");


            htmlstring.Replace("\r\n", "");


            #endregion


            return htmlstring;


        }


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


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


            #endregion
        }


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




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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值