关闭

字符取中en长度

456人阅读 评论(0) 收藏 举报

        /// <summary>
        /// 裁剪字符
        /// </summary>
        /// <param name="input">原始字符串</param>
        /// <param name="maxLength">最大长度,以中文(两个字节)为最小单位</param>
        /// <param name="maxLength">提示字符</param>
        /// <returns>字符数组</returns>
        public static string CutMaxLength(String input, int maxLength, string tips)
        {
            if (input.Length > maxLength)
            {
                string resultString = string.Empty;

                Char[] charArr = input.ToCharArray();
                if ("中文".Length == 4)
                {
                    return input.Substring(0, maxLength);//非中文平台
                }

                int count = 0;
                for (int i = 0; i < maxLength; i++)
                {
                    resultString += charArr[i];
                    if (!IsChinese(charArr[i]))
                    {
                        maxLength++;
                        count += 1;
                        if (count == 2)
                        {
                            count = 0;
                        }
                    }
                }

                if (count == 1)
                {
                    return resultString.Substring(0, maxLength - 1)+ tips;
                }
                return resultString;
            }
            return input;
        }
        private static bool IsChinese(char testChar)
        {
            Encoding encoding = Encoding.GetEncoding("GB2312");
            if (encoding.GetByteCount(testChar.ToString()) == 2)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

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

<%
function gotTopic(str,strlen)
dim l,t,c, i
l=len(trim(str))
t=0
for i=1 to l
 c=Abs(Asc(Mid(str,i,1)))
 if c>255 then '如果是汉字为2个字节
  t=t+2
  else
  t=t+1
 end if
if t=strlen then
  gotTopic=left(str,i)&".."
exit for
elseif t>strlen then
  gotTopic=left(str,i-1)&".."
exit for
 end if
next
gotTopic=str
end function

%>
<%=gotTopic("12中5文634", 12)%>

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

    public static string TrimString(String input, int maxLength, char padChar)
    {
        int resultLength = 0;
        int resultIndex = 0;
        foreach (char temp in input)
        {
            resultIndex++;
            if (Encoding.GetEncoding("GB2312").GetByteCount(temp.ToString()) == 2)
            {
                resultLength = resultLength + 2;
            }
            else
            {
                resultLength = resultLength + 1;
            }
            if (resultLength == maxLength)
            {
                return input.Substring(0, resultIndex);
            }
            else if (resultLength > maxLength)
            {
                return input.Substring(0, resultIndex-1);
            }
        }
        return input + string.Empty.PadRight(maxLength - resultLength, padChar);
    }

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:1405912次
    • 积分:20198
    • 等级:
    • 排名:第413名
    • 原创:448篇
    • 转载:1067篇
    • 译文:4篇
    • 评论:143条
    文章存档
    最新评论