博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
asp.net截取字符串方法
阅读量:6694 次
发布时间:2019-06-25

本文共 3151 字,大约阅读时间需要 10 分钟。

public partial class SubString : System.Web.UI.Page

    {
        protected void Page_Load(object sender, EventArgs e)
        {
            this.lblSub.Text = GetFirstString("中国人chinese", 7);
            this.lblSub2.Text = GetFirstString("afafaf你好", 7);
        }
  
        public  string getStr(string strInput, int intLen)
        {
            strInput = strInput.Trim();
            byte[] myByte = System.Text.Encoding.Default.GetBytes(strInput);
            Response.Write("getStr Function is::"+myByte.Length.ToString());
            if (myByte.Length > intLen)
            {
                //截取操作
                string resultStr = "";
                for (int i = 0; i < strInput.Length; i++)
                {
                    byte[] tempByte = System.Text.Encoding.Default.GetBytes(resultStr);
                    if (tempByte.Length < intLen)
                    {
                        resultStr += strInput.Substring(i, 1);
                    }
                    else
                    {
                        break;
                    }
                }
                return resultStr + "...";
            }
            else
            {
                return strInput;
            }
        }

        public  string cutString(string strInput, int intLen)

        {
            strInput = strInput.Trim();
            byte[] myByte = System.Text.Encoding.Default.GetBytes(strInput);
            Response.Write("cutString Function is::" + myByte.Length.ToString());
            if (myByte.Length > intLen)
            {
                //截取操作
                string resultStr = "";
                for (int i = 0; i < strInput.Length; i++)
                {
                    byte[] tempByte = System.Text.Encoding.Default.GetBytes(resultStr);
                    if (tempByte.Length < intLen)
                    {
                       
                        resultStr += strInput.Substring(i, 1);
                    }
                    else
                    {
                        break;
                    }
                }
                return resultStr + "...";
            }
            else
            {
                return strInput;
            }
        }

        public static string Fix(string s, int len)

        {
            string result = ""; //最终返回的结果
            int byteLen = System.Text.Encoding.Default.GetByteCount(s);  //单字节字符长度
            int charLen = s.Length; //把字符平等对待时的字符串长度
            int byteCount = 0;  //记录读取进度{中文按两单位计算}
            int pos = 0;    //记录截取位置{中文按两单位计算}
            if (byteLen > len)
            {
                for (int i = 0; i < charLen; i++)
                {
                    if (Convert.ToInt32(s.ToCharArray()[i]) > 255)  //遇中文字符计数加2
                        byteCount += 2;
                    else         //按英文字符计算加1
                        byteCount += 1;
                    if (byteCount >= len)   //到达指定长度时,记录指针位置并停止
                    {
                        pos = i;
                        break;
                    }
                }
                result = s.Substring(0, pos)+"...";
            }
            else
                result = s;

            return result;

        }

        #region  截短字串的函数,分区中英文

        /// <summary>
        /// 截短字串的函数
        /// </summary>
        /// <param name="mText">要加工的字串</param>
        /// <param name="byteCount">长度</param>
        /// <returns>被加工过的字串</returns>
        public string Left(string mText, int byteCount)
        {
            if (byteCount < 1) return mText;

            if (System.Text.Encoding.Default.GetByteCount(mText) <= byteCount)

            {
                return mText;
            }
            else
            {
                byte[] txtBytes = System.Text.Encoding.Default.GetBytes(mText);
                byte[] newBytes = new byte[byteCount - 4];

                for (int i = 0; i < byteCount - 4; i++)

                {
                    newBytes[i] = txtBytes[i];
                }

                return System.Text.Encoding.Default.GetString(newBytes) + "...";

            }
        }
        #endregion

        public static string GetFirstString(string stringToSub, int length)

        {
            Regex regex = new Regex("[\u4e00-\u9fa5]+", RegexOptions.Compiled);
            char[] stringChar = stringToSub.ToCharArray();
            StringBuilder sb = new StringBuilder();
            int nLength = 0;
            bool isCut = false;
            for (int i = 0; i < stringChar.Length; i++)
            {
                if (regex.IsMatch((stringChar[i]).ToString()))
                {
                    sb.Append(stringChar[i]);
                    nLength += 2;
                }
                else
                {
                    sb.Append(stringChar[i]);
                    nLength = nLength + 1;
                }

                if (nLength > length)

                {
                    isCut = true;
                    break;
                }
            }
            if (isCut)
                return sb.ToString() + "...";
            else
                return sb.ToString();
        }
    } 

 

.Net 技术探讨群 4151320

欢迎大家加入!!!

 

分类: 
本文转自快乐就好博客园博客,原文链接:http://www.cnblogs.com/happyday56/archive/2008/03/12/1101627.html,如需转载请自行联系原作者
你可能感兴趣的文章
夏日葵电商:微信商城初步搭建,如何提高产品转化率
查看>>
利用vue-cli配合vue-router搭建一个完整的spa流程(一)
查看>>
Microsoft推出适用于Win 8.1和Win10的KB 4010250 Flash Player更新
查看>>
JS的内置对象系列:Array(一)
查看>>
V8 使用“常量折叠”优化技巧,导致幂(**)运算有时候不等于 Math.pow()
查看>>
前端开源项目周报0228
查看>>
PHP_DOC 实时生成 API 文档
查看>>
微软宣布开源WPF、WinForms和WinUI
查看>>
Firefox 46解决安全问题,改善性能
查看>>
Gilt如何将微服务部署到AWS环境,介绍ION-Roller
查看>>
携程对AIOps场景和算法的探索与实践
查看>>
JavaOne 2016——首日亮点
查看>>
用WinForm/WPF代码来为.NET Core 3.0功能投票
查看>>
BAT集体升级云事业部,这背后都藏着哪些“小心思”?
查看>>
Clojure 1.7引入Transducers,提高跨平台支持度
查看>>
QCon北京2015精彩内容前瞻:邀请100余位一线专家,涵盖大数据、云计算、移动等20余个专题...
查看>>
京东私有云建设:挑战与应对之道
查看>>
入门解读:小白也能看懂的容器和虚拟机介绍
查看>>
微软宣布在Azure API管理中预览OpenAPI规范V3
查看>>
Studio 3T:MongoDB SQL探究
查看>>