zoukankan      html  css  js  c++  java
  • C# 截取字符串方法(包含中英混合)

     1 private string GetByteString(string center, int maxlen, string endStr)
     2         {
     3             string temp = center.Substring(0, (center.Length < maxlen + 1) ? center.Length : maxlen + 1);
     4             byte[] encodedBytes = System.Text.ASCIIEncoding.ASCII.GetBytes(temp);
     5             string outputStr = "";
     6             int count = 0;
     7             for (int i = 0; i < temp.Length; i++)
     8             {
     9                 if ((int)encodedBytes[i] == 63)
    10                     count += 2;
    11                 else
    12                     count += 1;
    13 
    14                 if (count <= maxlen - endStr.Length)
    15                     outputStr += temp.Substring(i, 1);
    16                 else if (count > maxlen)
    17                     break;
    18             }
    19             if (count <= maxlen)
    20             {
    21                 outputStr = temp;
    22                 endStr = "";
    23             }
    24             outputStr += endStr;
    25             return outputStr;
    26         }
  • 相关阅读:
    {purple8}
    {purple7}
    {暴力}
    uva1103(dfs)
    {purple5练习题}
    c++复习题
    关于继承
    lrj紫书第五章
    20个Linux系统监视工具
    linux上配置jdk+Apache
  • 原文地址:https://www.cnblogs.com/yunfeng83/p/4929343.html
Copyright © 2011-2022 走看看