培养良好的习惯,每天一点一滴的进步,终将会有收获。
/// <summary> /// 判断传入的字符串是否是为整数 /// </summary> /// <param name="strContent">待判断的字符串</param> /// <returns>判断结果</returns> public static bool IsInteger(string strContent) { bool IsInt = false; int result; if (!System.String.IsNullOrEmpty(strContent)) { IsInt = int.TryParse(strContent, out result); } return IsInt; } /// <summary> /// 将传入的字符串转换为整数,如果不是则返回默认值 /// </summary> /// <param name="strContent">待转换的字符串</param> /// <param name="defaultValue">转换失败的默认值</param> /// <returns>判断结果</returns> public static int GetInteger(string strContent, int defaultValue) { int result = defaultValue; if (!System.String.IsNullOrEmpty(strContent)) { bool isInt = int.TryParse(strContent, out result); if (!isInt) { result = defaultValue; } } return result; } /// <summary> /// 截取中文字符串 /// </summary> /// <param name="inputString">原始字符串</param> /// <param name="length">截取长度</param> /// <returns>截取后的字符串</returns> public static string SubString(string inputString, int length) { if (Encoding.UTF8.GetByteCount(inputString) <= length * 2) { return inputString; } 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; } tempString += inputString.Substring(i, 1); if (tempLen >= (length - 1) * 2) break; } //如果截过则加上半个省略号 if (System.Text.Encoding.Default.GetBytes(inputString).Length > length) tempString += "..."; return tempString; } /// <summary> /// 截取字符串 /// </summary> /// <param name="input">待截取的字符串</param> /// <param name="length">从字符串最后面去掉的位数</param> /// <returns></returns> public string CutString(string input, int length) { string result = string.Empty; if (input == null) return string.Empty; if (input.Length > length) { result = input.Substring(0, input.Length - length); } return result; }