stringhelper.cs

来自「1、用SQL查询器打开install目录下的dooogo.sql运行之后创建数据」· CS 代码 · 共 506 行 · 第 1/2 页

CS
506
字号
		{
			if(str==checkStr)
			{
				return reStr;
			}
			return "";
		}
		/// <summary>
		/// 截取左边规定字数字符串
		/// </summary>
		/// <param name="str">需截取字符串</param>
		/// <param name="length">截取字数</param>
		/// <param name="endStr">超过字数,结束字符串,如"..."</param>
		/// <returns>返回截取字符串</returns>
		public static string GetLeftStr(string str,int length,string endStr)
		{
			string reStr;
			if (length<GetStrLength(str))
			{
				reStr=str.Substring(0,length)+endStr;
			}
			else
			{
				reStr=str;
			}
			return reStr;
		}
		/// <summary>
		/// 截取左边规定字数字符串
		/// </summary>
		/// <param name="str">需截取字符串</param>
		/// <param name="length">截取字数</param>
		/// <returns>返回截取字符串</returns>
		public static string GetLeftStr(string str,int length)
		{
			string reStr;
			if (length<str.Length)
			{
				reStr=str.Substring(0,length)+"...";
			}
			else
			{
				reStr=str;
			}
			return reStr;
		}
		/// <summary>
		/// 获得双字节字符串的字节数
		/// </summary>
		/// <param name="str">要检测的字符串</param>
		/// <returns>返回字节数</returns>
		public static int GetStrLength(string str)
		{
			ASCIIEncoding n = new ASCIIEncoding();
			byte[] b = n.GetBytes(str);
			int l = 0;  // l 为字符串之实际长度
			for (int i=0;i < b.Length;i++)
			{
				if (b[i] ==63)  //判断是否为汉字或全脚符号
				{
					l++;
				}
				l++;
			}
			return l;
		}
		/// <summary>
		/// 剥去HTML标签
		/// </summary>
		/// <param name="text"></param>
		/// <returns></returns>
		public static string RegStripHtml(string text)
		{
			string reStr;
			string RePattern=@"<\s*(\S+)(\s[^>]*)?>";
			reStr=Regex.Replace(text,RePattern,string.Empty,RegexOptions.Compiled);
			reStr=Regex.Replace(reStr,@"\s+",string.Empty,RegexOptions.Compiled);
			return reStr;
		}
		/// <summary>
		/// 转换HTML与相对去处相对标签 未测试
		/// </summary>
		/// <param name="text"></param>
		/// <param name="ReLabel"></param>
		/// <returns></returns>
		public static string RegStripHtml(string text,string[] ReLabel)
		{
			string reStr=text;
			string LabelPattern=@"<({0})\s*(\S+)(\s[^>]*)?>[\s\S]*<\s*\/\1\s*>";
			string RePattern=@"<\s*(\S+)(\s[^>]*)?>";
			for(int i=0;i<ReLabel.Length;i++)
			{
				reStr=Regex.Replace(reStr,string.Format(LabelPattern,ReLabel[i]),string.Empty,RegexOptions.IgnoreCase);
			}
			reStr=Regex.Replace(reStr,RePattern,string.Empty,RegexOptions.Compiled);
			reStr=Regex.Replace(reStr,@"\s+",string.Empty,RegexOptions.Compiled);
			return reStr;
		}
		/// <summary>
		/// 使Html失效,以文本显示
		/// </summary>
		/// <param name="str">原字符串</param>
		/// <returns>失效后字符串</returns>
		public static string ReplaceHtml(string str)
		{
			str=str.Replace("<","&lt"); 
			return str;
		}
		/// <summary>
		/// 获得随机数字
		/// </summary>
		/// <param name="Length">随机数字的长度</param>
		/// <returns>返回长度为 Length 的 <see cref="System.Int32"/> 类型的随机数</returns>
		/// <example>
		/// Length 不能大于9,以下为示例演示了如何调用 GetRandomNext:<br />
		/// <code>
		///		int le = GetRandomNext(8);
		/// </code>
		/// </example>
		public static int GetRandomNext(int Length)
		{
			if(Length>9)
				throw new System.IndexOutOfRangeException("Length的长度不能大于10");
			Guid gu = Guid.NewGuid();
			string str = "";
			for(int i=0;i<gu.ToString().Length;i++)
			{
				if(isNumber(gu.ToString()[i]))
				{
					str +=((gu.ToString()[i]));
				}
			}
			int guid = int.Parse(str.Replace("-","").Substring(0,Length));
			if(!guid.ToString().Length.Equals(Length))
				guid = GetRandomNext(Length);
			return guid;
		}
		/// <summary>
		/// 返回一个 bool 值,指明提供的值是不是整数
		/// </summary>
		/// <param name="obj">要判断的值</param>
		/// <returns>true[是整数]false[不是整数]</returns>
		/// <remarks>
		///		isNumber 只能判断正(负)整数,如果 obj 为小数则返回 false;
		/// </remarks>
		/// <example>
		/// 下面的示例演示了判断 obj 是不是整数:<br />
		/// <code>
		///		bool flag;
		///		flag = isNumber("200");
		/// </code>
		/// </example>
		public static bool isNumber(object obj)
		{
			//为指定的正则表达式初始化并编译 Regex 类的实例
			System.Text.RegularExpressions.Regex rg = new System.Text.RegularExpressions.Regex(@"^-?(\d*)$");
			//在指定的输入字符串中搜索 Regex 构造函数中指定的正则表达式匹配项
			System.Text.RegularExpressions.Match mc = rg.Match(obj.ToString());
			//指示匹配是否成功
			return(mc.Success);
		}	
		/// <summary>
		/// 高亮显示
		/// </summary>
		/// <param name="str">原字符串</param>
		/// <param name="findstr">查找字符串</param>
		/// <param name="cssclass">Style</param>
		/// <returns></returns>
		public static string OutHighlightText(string str, string findstr, string cssclass)
		{
			if (findstr != "")
			{
				string text1 = "<span class=\"" + cssclass + "\">%s</span>";
				str = str.Replace(findstr, text1.Replace("%s", findstr));
			}
			return str;
		}
		/// <summary>
		/// 去除html标签
		/// </summary>
		/// <param name="str"></param>
		/// <returns></returns>
		public static string OutHtmlText(string str)
		{
			string text1 = "<.*?>";
			Regex regex1 = new Regex(text1);
			str = regex1.Replace(str, "");
			str=str.Replace("[$page]","");
			str=str.Replace("&nbsp;","");
			return ToHtmlText(str);
		}
		/// <summary>
		/// 将html显示成文本
		/// </summary>
		/// <param name="str"></param>
		/// <returns></returns>
		public static string ToHtmlText(string str)
		{
			if (str == "")
			{
				return "";
			}
			StringBuilder builder1 = new StringBuilder();
			builder1.Append(str);
			builder1.Replace("<", "&lt;");
			builder1.Replace(">", "&gt;");
			//builder1.Replace("\r", "<br>");
			return builder1.ToString();
		}
		/// <summary>
		/// 截取字符串
		/// </summary>
		/// <param name="strInput">输入字符串</param>
		/// <param name="intLen"></param>
		/// <returns></returns>
		public static string CutString(string strInput, int intLen)
		{
			strInput = strInput.Trim();
			byte[] buffer1 = Encoding.Default.GetBytes(strInput);
			if (buffer1.Length > intLen)
			{
				string text1 = "";
				for (int num1 = 0; num1 < strInput.Length; num1++)
				{
					byte[] buffer2 = Encoding.Default.GetBytes(text1);
					if (buffer2.Length >= (intLen - 4))
					{
						break;
					}
					text1 = text1 + strInput.Substring(num1, 1);
				}
				return (text1 + "...");
			}
			return strInput;
		}
		/// <summary>
		/// 根据条件返回值
		/// </summary>
		/// <param name="ifValue"></param>
		/// <param name="trueValue"></param>
		/// <param name="falseVale"></param>
		/// <returns></returns>
		public static string IfValue(bool ifValue, string trueValue, string falseVale)
		{
			if (ifValue)
			{
				return trueValue;
			}
			return falseVale;
		}
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?