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

  • 相关阅读:
    测试用例设计方法
    软件测试--常考的证书
    fillder-----接口延时返回(新方法)
    测试工作之--adb代码
    设计测试用例的常用方法
    TFS2013 设置签出独占锁
    Sql Server (MSSQLSERVER) 服务无法启动
    Sql 四大排名函数(ROW_NUMBER、RANK、DENSE_RANK、NTILE)简介
    C#使用redis学习笔记
    C#使用mybatis学习笔记
  • 原文地址:https://www.cnblogs.com/DTWolf/p/4670593.html
Copyright © 2011-2022 走看看