asp.net截取字符串方法

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();
        }
    } 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章