📄 base.cs
字号:
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Text.RegularExpressions;
using System.Xml;
using System.Web;
using System.Xml.Xsl;
using System.IO;
namespace Base
{
public class Base
{
//
public static bool RegEx(string exp, string str)
{
try
{
return Regex.IsMatch(str, exp);
}
catch
{
return false;
}
}
/// <summary>
/// 得到以字节为单位的字符串长度是否在min与max指定的长度之间
/// </summary>
/// <param name="str"></param>
/// <param name="min"></param>
/// <param name="max"></param>
/// <returns></returns>
public static bool CheckLenght(string str, int min, int max)
{
string dd = Regex.Replace(str, "[\u4E00-\u9FA5]", "aa");
int lenther = dd.Length;
if (lenther < min || lenther > max)
return false;
else
{
return true;
}
}
/// <summary>
/// 去除超链接标记
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static string DelLink(string str)
{
if (str != null && str.Trim() != "")
return Regex.Replace(str, "(<a[^>]+>)|(</a *>)", "");
return str;
}
public static string delHtml(string str)
{
if (str != null && str.Trim() != "")
return Regex.Replace(str, "<[^>]+>", "");
return str;
}
/// <summary>
/// 删除标记
/// </summary>
/// <param name="tag"></param>
/// <param name="isContent">是否清除内容</param>
/// <returns></returns>
public static string delTag(string str, string tag, bool isContent)
{
if (tag == null || tag == "")
{
return str;
}
// "<a((.*?)("[^>]*?")(.*?))*>"
if (isContent)
{
return Regex.Replace(str, string.Format(@"<({0})[^>]*>(.*?)<\/\1>", tag), "", RegexOptions.IgnoreCase);
//return Regex.Replace(str, string.Format("<a((.*?)(\"[^>]*?\")(.*?))*>([\\s\\S]*?)<\\/\\1>", tag), "", RegexOptions.IgnoreCase);
}
return Regex.Replace(str, string.Format(@"(<{0}[^>]*(>)?)|(</{0}[^>]*>)|", tag), "", RegexOptions.IgnoreCase);
//return Regex.Replace(str, string.Format("(<{0}((.*?)(\"[^>]*?\")(.*?))*>)|(</{0}[^>]*>)", tag), "", RegexOptions.IgnoreCase);
}
public static string delTagArray(string str, string tagA, bool isContent)
{
string[] tagAa = tagA.Split(',');
foreach (string sr1 in tagAa)
{
str = delTag(str, sr1, isContent);
}
return str;
}
/// <summary>
/// 删除除本标记外的所有标记
/// </summary>
/// <param name="str"></param>
/// <param name="tag"></param>
/// <returns></returns>
public static string delNotTag(string str, string tag, bool isContent)
{
return Regex.Replace(str, string.Format(@"(<(?!{0})[^>]*(>)?)|(</(?!{0})[^>]*>)|", tag), "", RegexOptions.IgnoreCase);
}
public static string delNotTag(string str, string tag)
{
//MatchCollection mc=Regex.Matches(str,
return "";
}
/// <summary>
/// 删除控制字符
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static string delOption(string str)
{
return Regex.Replace(str, @"(\n)|(\t)|(\r)|(\v)|(\f)", "");
}
/// <summary>
/// 删除最处层的HTML标记
/// </summary>
/// <param name="str"></param>
/// <param name="tag"></param>
/// <returns></returns>
public static string delRLTag(string str, string tag)
{
if (tag == null || tag == "")
{
return str;
}
string zz = Regex.Match(str, string.Format("(?<=<{0}[^>]*?>)[\\s\\S]*(?=</{0}[^>]*?>)", tag), RegexOptions.IgnoreCase).Value;
if (zz == null || zz.Trim() == "")
return str;
return zz;
}
/// <summary>
/// 删除空格
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static string delSpanTag(string str)
{
return Regex.Replace(str, @"[^<]\s", "");
}
/// <summary>
/// 得到一个匹配模式
/// </summary>
/// <param name="input"></param>
/// <param name="patt"></param>
/// <returns></returns>
public static Match Match(string input, string patt)
{
return Regex.Match(input, patt, RegexOptions.IgnoreCase);
}
/// <summary>
/// 得到所有的匹配
/// </summary>
/// <param name="input"></param>
/// <param name="patt"></param>
/// <returns></returns>
public static MatchCollection MatchCollection(string input, string patt)
{
return Regex.Matches(input, patt, RegexOptions.IgnoreCase);
}
public static string Replace(string input, string patt, string replacetext)
{
return Regex.Replace(input, patt, replacetext, RegexOptions.IgnoreCase);
}
//得到虚拟路径的目录部分
public static string VirtualPath(string input)
{
string yy = Regex.Replace(input, @"^(\w+://[^/]+)(.*?)(([^/]+(?=\?))|(([^/]+)$))(.*)", "${1}${2}");
if (yy == null || yy == "")
return input;
return yy;
}
/// <summary>
/// 得到Post的数据
/// </summary>
/// <returns></returns>
public static string getpost()
{
HttpRequest req = HttpContext.Current.Request;
StringBuilder sb = new StringBuilder();
string[] keyall = req.Form.AllKeys;
foreach (string str in keyall)
{
sb.AppendFormat("{0}={1}&", str, req.Form[str]);
}
if (sb.ToString().EndsWith("&"))
{
sb.Remove(sb.Length - 1, 1);
}
return sb.ToString();
}
public static string getApplicationPath()
{
string path = HttpContext.Current.Request.ApplicationPath;
if (path.EndsWith("/"))
{
return path;
}
else
{
return path + "/";
}
}
public static string getpage(int sum, int curr, int sumcunt)
{
if (sum == 0 || curr == 0 || sumcunt == 0)
return "";
string formstr = "<a href='javascript:go({0},{2})'>上一页</a> | <a href='javascript:go({1},{2})'>下一页</a> | <a href='javascript:go({2},{2})'>末页</a> | 第{3}页 | 共{4}页 | 共{5}行";
return string.Format(formstr, curr - 1, curr + 1, sum, curr, sum, sumcunt);
}
public static string ToHtml(string str)
{
str = str.Replace("&", "&");
return str;
}
/// <summary>
/// 转换
/// </summary>
/// <param name="ds">DataSet</param>
/// <param name="filePath">相对于网站根的路径</param>
/// <returns></returns>
public static string XmlToString(DataSet ds,string filePath)
{
XmlDataDocument xdd = new XmlDataDocument(ds);
XmlWriterSettings settings = new XmlWriterSettings();
settings.OmitXmlDeclaration = true;
settings.ConformanceLevel = ConformanceLevel.Fragment;
settings.CloseOutput = false;
StringBuilder sb = new StringBuilder();
TextWriter tw = new StringWriter(sb);
XmlWriter xw = XmlWriter.Create(tw,settings);
XslCompiledTransform xst = new XslCompiledTransform();
xst.Load(HttpContext.Current.Server.MapPath(Base.getApplicationPath() + filePath));
xst.Transform(xdd, xw);
return sb.ToString();
}
public static string XmlToString(XmlDocument xml, string filePath)
{
XmlWriterSettings settings = new XmlWriterSettings();
settings.OmitXmlDeclaration = true;
settings.ConformanceLevel = ConformanceLevel.Fragment;
settings.CloseOutput = false;
StringBuilder sb = new StringBuilder();
TextWriter tw = new StringWriter(sb);
XmlWriter xw = XmlWriter.Create(tw, settings);
XslCompiledTransform xst = new XslCompiledTransform();
xst.Load(HttpContext.Current.Server.MapPath(Base.getApplicationPath() + filePath));
xst.Transform(xml, xw);
return sb.ToString();
}
/// <summary>
/// 得到post的数据
/// </summary>
/// <param name="postname"></param>
/// <param name="patt"></param>
/// <returns></returns>
///
public static string getPostItem(string postname)
{
return getPostItem(postname,"","");
}
public static string getPostItem(string postname, string patt, string replstr)
{
try
{
string result = HttpContext.Current.Request[postname];
if (patt == null || patt.Trim() == "")
{
if (result == null)
{
return "";
}
return result;
}
return Base.Replace(result, patt, replstr);
}
catch
{
return "";
}
}
/// <summary>
/// 格式化表格
/// </summary>
/// <param name="str"></param>
/// <param name="index"></param>
/// <returns></returns>
public static string FormTable(string str, int index,string titleclss,string itemclass)
{
str = Base.Replace(str, "(?<=<table\\s*)[^>]*", "");
str = Base.Replace(str, "(?<=<tr\\s*)[^>]*", "");
str = Base.Replace(str, "(?<=<td\\s*)[^>]*", "");
MatchCollection mcl = MatchCollection(str, "<tr[\\s\\S]*?</tr>");
StringBuilder sb = new StringBuilder();
sb.Append("<table>");
if (mcl.Count >=index)
{
sb.Append(Replace(mcl[index - 1].Value, "(?<=<td[^>]*)[^>]*", string.Format(" class='{0}'", titleclss)));
for (int i = index; i < mcl.Count; i++)
{
sb.Append(Replace(mcl[i].Value, "(?<=<td[^>]*)[^>]*", string.Format(" class='{0}'", itemclass)));
}
}
else
{
return str;
}
sb.Append("</table>");
return sb.ToString();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -