国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

StringHelper類,內容截取,特別適合資訊展示列表

2019-11-17 01:26:30
字體:
來源:轉載
供稿:網友

StringHelper類,內容截取,特別適合資訊展示列表

public class StringHelper { /// <summary> /// 截字符串 /// </summary> /// <param name="sInString">字符串</param> /// <param name="iCutLength">截幾個字</param> /// <returns>截好的字符串</returns> public static string CutStr(object sInStrings, int iCutLength) {

string sInString = sInStrings.ToString(); if (sInString == null || sInString.Length == 0 || iCutLength <= 0) return ""; int iCount = System.Text.Encoding.Default.GetByteCount(sInString); if (iCount > iCutLength) { int iLength = 0; for (int i = 0; i < sInString.Length; i++) { int iCharLength = System.Text.Encoding.Default.GetByteCount(new char[] { sInString[i] }); iLength += iCharLength; if (iLength == iCutLength) //需要截取的和總字符串位數相等 { sInString = sInString.Substring(0, i + 1); break; } else if (iLength > iCutLength)//需要截取的小于總字符長度 { sInString = sInString.Substring(0, i) + "...";//。。。 break; } } } return sInString; }

/// <summary> /// 按字節長度截取字符串(支持截取帶HTML代碼樣式的字符串) /// </summary> /// <param name=”param”>將要截取的字符串參數</param> /// <param name=”length”>截取的字節長度</param> /// <param name=”end”>字符串末尾補上的字符串</param>此處寫死了,可放開 /// <returns>返回截取后的字符串</returns> public static string SubstringToHTML(string param, int length) { string end = "..."; string Pattern = null; MatchCollection m = null; StringBuilder result = new StringBuilder(); int n = 0; char temp; bool isCode = false; //是不是HTML代碼 bool isHTML = false; //是不是HTML特殊字符,如&nbsp; char[] pchar = param.ToCharArray(); for (int i = 0; i < pchar.Length; i++) { temp = pchar[i]; if (temp == '<') { isCode = true; } else if (temp == '&') { isHTML = true; } else if (temp == '>' && isCode) { n -= 1; isCode = false; } else if (temp == ';' && isHTML) { isHTML = false; }

if (!isCode && !isHTML) { n = n + 1; //UNICODE碼字符占兩個字節 if (System.Text.Encoding.Default.GetBytes(temp + "").Length > 1) { n = n + 1; } }

result.Append(temp); if (n >= length) { break; } } result.Append(end); //取出截取字符串中的HTML標記 string temp_result = result.ToString().Replace("(>)[^<>]*(<?)", "$1$2"); //去掉不需要結素標記的HTML標記 temp_result = temp_result.Replace(@"</?(AREA|BASE|BASEFONT|BODY|BR|COL|COLGROUP|DD|DT|FRAME|HEAD|HR|HTML

|IMG|INPUT|ISINDEX|LI|LINK|META|OPTION|P|PARAM|TBODY|TD|TFOOT|TH|THEAD

|TR|area|base|basefont|body|br|col|colgroup|dd|dt|frame|head|hr|html|img|input|isindex|li|link|meta

|option|p|param|tbody|td|tfoot|th|thead|tr)[^<>]*/?>", ""); //去掉成對的HTML標記 temp_result = temp_result.Replace(@"<([a-zA-Z]+)[^<>]*>(.*?)</1>", "$2"); //用正則表達式取出標記 Pattern = ("<([a-zA-Z]+)[^<>]*>"); m = Regex.Matches(temp_result, Pattern); ArrayList enDHTML = new ArrayList(); foreach (Match mt in m) { endHTML.Add(mt.Result("$1")); } //補全不成對的HTML標記 for (int i = endHTML.Count - 1; i >= 0; i--) { result.Append("</"); result.Append(endHTML[i]); result.Append(">"); } return result.ToString(); }

}}


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 陇川县| 石门县| 济源市| 蒲江县| 东莞市| 光山县| 凤阳县| 贵溪市| 永靖县| 运城市| 历史| 宁海县| 塔河县| 古丈县| 柏乡县| 关岭| 云龙县| 阳春市| 霍邱县| 通渭县| 怀集县| 抚松县| 镇雄县| 长葛市| 峨山| 岐山县| 南通市| 古丈县| 绥阳县| 江都市| 宁国市| 商城县| 宝坻区| 广宗县| 崇义县| 安多县| 满洲里市| 德昌县| 奉新县| 广水市| 桂林市|