asp.net 截取字符


    #region 截取字符
    /// <summary>
    /// 截取字符
    /// </summary>
    /// <param name="Str">要截取的字符串</param>
    /// <param name="StrLen">要截取的长度</param>
    /// <returns>返回截取后的字符串</returns>
    public static string CutStr(string Str, int StrLen)
    {
        if (Str != null && Convert.IsDBNull(StrLen) == false)
        {
            int t = 0;
            for (int i = 0; i <= StrLen; i++)
            {
                if (t >= StrLen)
                {
                    Str = Str.Substring(0, i) + "..";
                    break;
                }
                else
                {
                    if (i >= Str.Length)
                    {
                        Str = Str.Substring(0, i);
                        break;
                    }
                }
                int c = Math.Abs((int)Str[i]);
                if (c > 255) //判断中英文
                {
                    t += 2;
                }
                else
                {
                    t++;
                }
            }
        }
        else
        {
            Str = null;
        }
        return Str;
    }
    #endregion

 

======================================================================================

 

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

posted @ 2009-04-28 15:53  .NET钉子户  阅读(438)  评论(0)    收藏  举报