Common下StringHelper.cs

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

namespace Common
{
    public class StringHelper
    {
        public static string SubStr(string sString, int nLeng)
        {
            if (sString.Length <= nLeng)
            {
                return sString;
            }
            else
            {
                return sString.Substring(0, nLeng) + "...";
            }
        }

        /// <summary>
        /// 清除脚本
        /// </summary>
        /// <param name="Htmlstring"></param>
        /// <returns></returns>
        public static string NoHTML(string Htmlstring)
        {
            //删除脚本 
            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", "");
            Htmlstring = HttpContext.Current.Server.HtmlEncode(Htmlstring).Trim();
            return Htmlstring;
        }
        /// <summary>
        /// 生成指定长度的随机数字串
        ///</summary>
        ///<param name="length">长度</param>
        ///<returns></returns>
       public  static string RndStr(int length)
        {
            Random rnd = new Random();
            string returnstring = "";
            int i = 1;
            while (i <= length)
            {
                returnstring += rnd.Next(0, 9).ToString();
                i++;
            }
            return returnstring;
        }

 

/// <summary>
        /// 截取字符串,保留其HTML格式
        /// </summary>
        /// <param name="strText"></param>
        /// <param name="len"></param>
        /// <returns></returns>
        public static string CutWithSubstring(string strText, int len)
        {
            if (strText.Length > len)
            {
                return strText.Substring(0, len) + "…";
            }
            else
            {
                return strText;
            }
        }

 

 

public static string LoseHtml(string ContentStr)
        {
            string ClsTempLoseStr = "";
            System.Text.RegularExpressions.Regex regEx = new System.Text.RegularExpressions.Regex("<///*[^<>]*>");
            ClsTempLoseStr = regEx.Replace(ContentStr, "");
            return ClsTempLoseStr;
        }

 

 

 /// <summary>
        /// 截取字符串去掉其HTML字符
        /// </summary>
        /// <param name="inputString"></param>
        /// <param name="len"></param>
        /// <returns></returns>
        public static string CutWithOutHtml(string inputString, int len)
        {
            int tempLen = 0;
            string tempString = "";
            inputString = LoseHtml(inputString);
            ASCIIEncoding ascii = new ASCIIEncoding();

            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 = tempString.Substring(0, tempString.Length - 3 == 0 ? 1 : tempString.Length - 3) + "…";
            }
            return tempString;
        }

 

 

    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
NullReferenceException: Object reference not set to an instance of an object BattleStageControl.OpenBattleRewardNext () (at Assets/0.C#/Battle/BattleStageControl.cs:102) BattleStageControl.OpenBattleReward () (at Assets/0.C#/Battle/BattleStageControl.cs:70) EventCenter.Broadcast (MeventType MeventType) (at Assets/0.C#/Common/EventCenter.cs:162) FuguiState.DeleteFugui02 (System.String site, System.String FuguiName) (at Assets/0.C#/Map/FuguiState.cs:231) EventCenter.Broadcast[T,X] (MeventType MeventType, T arg1, X arg2) (at Assets/0.C#/Common/EventCenter.cs:196) SaveDataManager.DeleteFugui01 (System.String site, System.String FuguiName) (at Assets/0.C#/Common/SaveDataManager.cs:517) EventCenter.Broadcast[T,X] (MeventType MeventType, T arg1, X arg2) (at Assets/0.C#/Common/EventCenter.cs:196) FuguiActionTriger.clickToDelete () (at Assets/0.C#/Battle/FuguiActionTriger.cs:519) UnityEngine.Events.InvokableCall.Invoke () (at <c6956609c5d74f5d8e6ed4e5e5f40cc2>:0) UnityEngine.Events.UnityEvent.Invoke () (at <c6956609c5d74f5d8e6ed4e5e5f40cc2>:0) UnityEngine.UI.Button.Press () (at Library/PackageCache/com.unity.ugui@1.0.0/Runtime/UI/Core/Button.cs:70) UnityEngine.UI.Button.OnPointerClick (UnityEngine.EventSystems.PointerEventData eventData) (at Library/PackageCache/com.unity.ugui@1.0.0/Runtime/UI/Core/Button.cs:114) UnityEngine.EventSystems.ExecuteEvents.Execute (UnityEngine.EventSystems.IPointerClickHandler handler, UnityEngine.EventSystems.BaseEventData eventData) (at Library/PackageCache/com.unity.ugui@1.0.0/Runtime/EventSystem/ExecuteEvents.cs:57) UnityEngine.EventSystems.ExecuteEvents.Execute[T] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.ExecuteEvents+EventFunction`1[T1] functor) (at Library/PackageCache/com.unity.ugui@1.0.0/Runtime/EventSystem/ExecuteEvents.cs:272) UnityEngine.EventSystems.EventSystem:Update() (at Library/PackageCache/com.unity.ugui@1.0.0/Runtime/EventSystem/EventSystem.cs:501)什么意思
最新发布
05-31

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值