用於剪下字串

iDotNetSpace發表於2009-07-23
///
    /// 用於剪下字串
    ///

    /// 字串
    /// 留字串的長度
    ///
    public static string CutStr(string sInString, int iCutLength)
    {
        if (sInString == null || sInString.Length == 0 || iCutLength <= 0)
            return "";
        int iCount = System.Text.Encoding.GetEncoding("GB2312").GetByteCount(sInString);
        if (iCount > iCutLength)
        {
            int iLength = 0;
            for (int i = 0; i < sInString.Length; i++)
            {
                int iCharLength = System.Text.Encoding.GetEncoding("GB2312").GetByteCount(new char[] { sInString[i] });
                iLength += iCharLength;
                if (iLength == iCutLength)
                {
                    sInString = sInString.Substring(0, i + 1);
                    break;
                }
                else if (iLength > iCutLength)
                {
                    sInString = sInString.Substring(0, i);
                    break;
                }
            }
        }
        return sInString;
    }

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/12639172/viewspace-609979/,如需轉載,請註明出處,否則將追究法律責任。

相關文章