c#字符串截取函数

mac2022-06-30  24

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

转载于:https://www.cnblogs.com/webman/archive/2007/06/20/790597.html

相关资源:C#中英文混合字符串截取函数
最新回复(0)