//region 字符串截取函数
//首先引入System.Text命名空间
    public static string CutString(string inputString,int len)
    {
        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) //63:表示问号:?
            {
                tempLen+=2;
            }
            else
            {
                tempLen+=1;
            }

            try
            {
                tempString+=inputString.Substring(i,1);
            }
            catch
            {
                break;
            }

            if(tempLen>=(len*2))
            break;
        }
        //如果截过则加上半个省略号
        byte[] mybyte=System.Text.Encoding.Default.GetBytes(inputString);
        if(mybyte.Length>len)
        tempString+="…"; 
        return tempString;
    }

相关文章:

  • 2022-12-23
  • 2021-08-18
  • 2022-12-23
  • 2021-11-13
  • 2021-12-26
猜你喜欢
  • 2021-06-19
  • 2021-10-31
  • 2021-12-23
  • 2022-12-23
  • 2021-09-22
相关资源
相似解决方案