/// <summary> /// 剪切指定长度的字符串,并去掉HTML标记 /// </summary> /// <param name="strr">要剪切字符串的Object形式</param> /// <param name="len">长度(中文长度为2)</param> /// <returns></returns> public static string CutStringX(object strr, int len) { string str = strr.ToString(); string strRet = ""; char CH; int nLen = str.Length; int nCutLen = 0; int nPos = 0; int bLeft = 0; int nChinese = 0; while (nPos < nLen && nCutLen < len) { CH = str[nPos]; nPos++; if (CH == '<') { bLeft++; continue; } if (CH == '>') { bLeft--; continue; } if (nCutLen == 0 && CH.ToString() == " " && CH.ToString() == "\n") { continue; } if (bLeft == 0) { //是否为中文 if (IsChinese(CH)) { nCutLen += 2; nChinese++; } else { nCutLen += 1; } strRet += CH; } } strRet = strRet.Replace(" ", " "); if (nPos < nLen) { strRet += ""; // strRet += "..."; } return(strRet); }
protected int CalculateEvenPositionDigits(string EighteenDigitNumber) { int sum = 0; string strConat = ""; const int MulBy = 2; for (int n = 0; n < EighteenDigitNumber.Length; n = n + 2) { //multiple even position digits by 2 long result = Convert.ToInt32(EighteenDigitNumber.Substring(n + 1, 1)) * MulBy; //Concatenate Them strConat = string.Concat(strConat, result.ToString()); } foreach (char CH in strConat) { //Add all the digits in the string sum += Convert.ToInt32(CH.ToString()); } return(sum); }