```csharp /// /// 截取指定长度字符串 /// /// 要处理的字符串 /// 指定长度 /// 返回处理后的字符串 public static string ClipString(string inputString, int len) { bool isShowFix = false; if (len % 2 == 1) { isShowFix = true; len--; } System.Text.ASCIIEncoding ascii = new System.Text.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; try { tempString += inputString.Substring(i, 1); } catch { break; } if (tempLen > len) break; } byte[] mybyte = System.Text.Encoding.Default.GetBytes(inputString); if (isShowFix && mybyte.Length > len) tempString += "…"; return tempString; } ``` Last modification:February 4, 2020 © Allow specification reprint Support Appreciate the author AliPayWeChat Like 1 请我喝一瓶农夫山泉嘛?
One comment
想想你的文章写的特别好https://www.237fa.com/