zoukankan      html  css  js  c++  java
  • Encoding encoding = Encoding.GetEncoding("gb2312"); 与byte[] ping = Encoding.UTF8.GetBytes(inputString);区别

     Encoding encoding = Encoding.GetEncoding("gb2312"); 与byte[] ping = Encoding.UTF8.GetBytes(inputString);区别

     public static string SubString(string inputString, int length)
            {
                byte[] ping = Encoding.UTF8.GetBytes(inputString);
                int count=Encoding.UTF8.GetByteCount(inputString);
                if (count <= length * 2)
                {
                    return inputString;
                }
                ASCIIEncoding ascii = new ASCIIEncoding();
                int tempLen = 0;
                string tempString = "";
                byte[] s = ascii.GetBytes(inputString);
                for (int i = 0; i < s.Length; i++)
                {
                    if ((int)s[i] == 63)
                    {
                        tempLen += 2;
                    }
                    else
                    {
                        tempLen += 1;
                    }
                    tempString += inputString.Substring(i, 1);
                    if (tempLen >= length * 2)
                        break;
                }
                return tempString;
            }

     public static string GetSub(string sub, int length)         {             if (sub == null) return string.Empty;             int len = length * 2;             //aequilateLength为中英文等宽长度,cutLength为要截取的字符串长度             int aequilateLength = 0, cutLength = 0;             Encoding encoding = Encoding.GetEncoding("gb2312");

                string cutStr = sub.ToString();             int strLength = cutStr.Length;             byte[] bytes;             for (int i = 0; i < strLength; i++)             {                 bytes = encoding.GetBytes(cutStr.Substring(i, 1));                 if (bytes.Length == 2)//不是英文                     aequilateLength += 2;                 else                     aequilateLength++;

                    if (aequilateLength <= len) cutLength += 1;

                    if (aequilateLength > len)                     return cutStr.Substring(0, cutLength);//+ "..."             }             return cutStr;         }

  • 相关阅读:
    Vue项目问题-TypeError: this.getOptions is not a function
    JavaScript学习-JavaScript 如何工作:对引擎、运行时、调用堆栈的概述
    JavaScript学习-理解JavaScript中的执行上下文和执行栈
    Element-ui中的给el-row添加一个gutter间隔不生效
    Vue-cil3 配置路径别名详解
    常见的内存泄漏以及解决方案
    Vue学习-组件之间的8种通信方式
    JavaScript学习-WeakMap和Map的区别,WeakMap的原理,为什么能被GC?
    Javascript学习-WeakMap
    JavaScript学习-Map
  • 原文地址:https://www.cnblogs.com/DTWolf/p/4670697.html
Copyright © 2011-2022 走看看