一个很实用的字符串截取函数

参考文章:http://hi.baidu.com/linrao/blog/item/82b9178a921cf11ac8fc7ada.html

网站设计中,为了版面好看,通常将新闻的标题长度不一样的截取为一样长度的,我也做网站,今天刚好碰上了,他写的不错,我直接拿来用了,关键的函数代码如下:

        public static string CutStringByByBytes(string SourceString, int CutLeng)
        {
            CutStr_Bytes1 = new byte[CutLeng];
            SourceStr_Bytes = myEncoding.GetBytes(SourceString);
            Bytes_Count = SourceStr_Bytes.Length;
            if (Bytes_Count > CutLeng)
            {
                Array.Copy(SourceStr_Bytes, 0, CutStr_Bytes1, 0, CutLeng);
                CutedStr = myEncoding.GetString(CutStr_Bytes1);
                CutedStr = CutedStr.Substring(0, CutedStr.Length - 1) + "...";
                return CutedStr;
            }
            return SourceString;
        }

我在本地windows程序上测试,效果如下:

 一个很实用的字符串截取函数

效果很好,赞一个!

posted @ 2010-11-28 19:13  crid  阅读(143)  评论(0编辑  收藏  举报