zoukankan      html  css  js  c++  java
  • c#的中英文混合字符串截取 public static string SubString(string inputString, int byteLength)

      /// <summary>
            /// c#的中英文混合字符串截取(区分中英文)
            /// </summary>
            /// <param name="inputString"></param>
            /// <param name="byteLength">要输出的字节长度</param>
            /// <returns></returns>
            public static string SubString(string inputString, int byteLen)
            {
                int count=Encoding.UTF8.GetByteCount(inputString);
                if (count <= byteLen * 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 >= byteLen * 2)
                        break;
                }
                return tempString;
            }

  • 相关阅读:
    [转]android刷新后R.java不见了
    adb常用指令
    [转]Intent跳转到系统应用中的拨号界面、联系人界面、短信界面及其他
    effective c/C++
    七种布局显示方式效果及实现
    修改Tabhost样式和字体大小的方法
    [转]android中SoundRecorder
    java中的IO整理
    在xp下面下载Android源代码
    linux网络 (二):无线网络操作
  • 原文地址:https://www.cnblogs.com/DTWolf/p/4670593.html
Copyright © 2011-2022 走看看