這個函數也沒有什么特別之處,就是可以截取一定長度的字符串,可能小特點就是len是字節,解決了漢字與英文字節不一樣導致直接截取到的長 度不一樣的問題,
#region 字符串截取函數 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) { 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(mybyte.Length>len) tempString+="…";
return tempString; } #endr
|