cxzhq2002的杂记

cxzhq2002的杂记

字符取中en长度

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

阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/cxzhq2002/article/details/1782074
想对作者说点什么? 我来说一句

C# 对身份证号进行处理

2008年08月02日 71KB 下载

没有更多推荐了,返回首页

不良信息举报

字符取中en长度

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭