using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Web;
namespace ETD.Data
{
public class Text
{
#region 清除给定字符串中的回车及换行符
///
/// 清除给定字符串中的回车及换行符
///
/// 要清除的字符串
/// 清除后返回的字符串
public static string ClearBR(string str)
{
Regex r = null;
Match m = null;
r = new Regex(@"(\r\n)", RegexOptions.IgnoreCase);
for (m = r.Match(str); m.Success; m = m.NextMatch())
{
str = str.Replace(m.Groups[0].ToString(), "");
}
return str;
}
#endregion
#region 清除html字符
public static string ClearHtml(string strHtml)
{
if (strHtml != "")
{
Regex r = null;
Match m = null;
r = new Regex(@"<\/?[^>]*>", RegexOptions.IgnoreCase);
for (m = r.Match(strHtml); m.Success; m = m.NextMatch())
{
strHtml = strHtml.Replace(m.Groups[0].ToString(), "");
}
}
return strHtml;
}
#endregion
#region 生成内码
///
/// 生成系统需要的内码
///
/// 返回一个22位的内码
public static string CodeBuild()
{
try
{
System.Threading.Thread.Sleep(15);
string code = DateTime.Now.ToString("yyyyMMddHHmmss") + DateTime.Now.Millisecond.ToString();
if (code.Length < 17)
{
for (int i = 0; i < 17 - code.Length; i++)
{
code = code + "0";
}
}
Random Rnd = new Random();
string ran = Rnd.Next(10000000, 99999999).ToString();
code = code + ran;
if (code.Length < 25)
{
for (int i = 0; i < 25 - code.Length; i++)
{
code = code + "8";
}
}
return code;
}
catch (Exception e)
{
throw (e);
}
}
#endregion
#region 字符串如果操过指定长度则将超出的部分用指定字符串代替
///
/// 字符串如果操过指定长度则将超出的部分用指定字符串代替
///
/// 要检查的字符串
/// 指定长度
/// 用于替换的字符串
/// 截取后的字符串
public static string CutString(string strStr, int strLength, string strReplace)
{
string strMyStr = strStr;
if (strLength < 0)
{
return strMyStr;
}
byte[] bsSrcString = Encoding.Default.GetBytes(strStr);
if (bsSrcString.Length <= strLength)
{
return strMyStr;
}
int nRealLength = strLength;
int[] anResultFlag = new int[strLength];
byte[] bsResult = null;
int nFlag = 0;
for (int i = 0; i < strLength; i++)
{
if (bsSrcString[i] > 0x7f)
{
nFlag++;
if (nFlag == 3)
{
nFlag = 1;
}
}
else
{
nFlag = 0;
}
anResultFlag[i] = nFlag;
}
if ((bsSrcString[strLength - 1] > 0x7f) && (anResultFlag[strLength - 1] == 1))
{
nRealLength = strLength + 1;
}
bsResult = new byte[nRealLength];
Array.Copy(bsSrcString, bsResult, nRealLength);
return (Encoding.Default.GetString(bsResult) + strReplace);
}
#endregion
#region 替换html字符
///
/// 替换html字符
///
public static string EncodeHtml(string strHtml)
{
if (strHtml != "")
{
strHtml = strHtml.Replace(",", "&def");
strHtml = strHtml.Replace("'", "&dot");
strHtml = strHtml.Replace(";", "&dec");
return strHtml;
}
return "";
}
#endregion
#region 生成指定位数的随机数
public static string GenerateRandNum(int Length, RandEnum randtype)
{
string code = "";
Random Rnd = new Random();
if (code.Length < Length)
{
switch (randtype)
{
case RandEnum.Int:
for (int i = 0; i < Length - code.Length; i++)
{
code = code + Rnd.Next(0, 9).ToString();
}
break;
case RandEnum.DateTime:
code = DateTime.Now.ToString("yyyyMMddHHmmss") + DateTime.Now.Millisecond.ToString();
for (int i = 0; i < Length - code.Length; i++)
{
code = code + Rnd.Next(0, 9).ToString();
}
break;
default:
break;
}
}
return code;
}
public enum RandEnum
{
Int,
DateTime
}
#endregion
#region 取页面Get传递的参数
///
/// 取URL传递的参数
///
///
///
public static string Get(string name)
{
string text1 = HttpContext.Current.Request.QueryString[name];
return ((text1 == null) ? string.Empty : text1.Trim());
}
///
/// 取URL传递的参数
///
/// 指定参数名
/// 类型
///
public static string Get(string name, CheckGetEnum chkType)
{
string text1 = Get(name);
bool flag1 = false;
switch (chkType)
{
case CheckGetEnum.Int:
flag1 = RegExp.IsNumber(text1);
break;
case CheckGetEnum.Safety:
flag1 = RegExp.IsSafety(text1);
break;
case CheckGetEnum.Json:
flag1 = RegExp.IsJsonSafety(text1);
break;
default:
flag1 = true;
break;
}
if (!flag1)
{
throw new Exception("hc1305");
}
return text1;
}
#endregion
#region 取提交Post页面指定值
///
/// 取提交页面指定值
///
///
///
public static string Post(string name)
{
string text1 = HttpContext.Current.Request.Form[name];
return ((text1 == null) ? string.Empty : text1.Trim());
}
///
/// 取提交页面指定值
///
///
/// 类型
///
public static string Post(string name, CheckGetEnum chkType)
{
string text1 = HttpContext.Current.Request.Form[name];
if (text1 == null)
{
return string.Empty;
}
bool flag1 = false;
switch (chkType)
{
case CheckGetEnum.Int:
flag1 = RegExp.IsNumber(text1);
break;
case CheckGetEnum.Safety:
flag1 = RegExp.IsSafety(text1);
break;
case CheckGetEnum.Json:
flag1 = RegExp.IsJsonSafety(text1);
break;
default:
flag1 = true;
break;
}
if (!flag1)
{
throw new Exception("hc1305");
}
return text1.Trim();
}
///
/// 枚举
///
public enum CheckGetEnum
{
Int,
Safety,
Json
}
#endregion
#region 返回标准日期格式string
///
/// 返回标准日期格式string
///
public static string GetDate()
{
return DateTime.Now.ToString("yyyy-MM-dd");
}
#endregion
#region 返回指定日期格式
///
/// 返回指定日期格式
///
public static string GetDate(string datetimestr, string replacestr)
{
if (datetimestr == null)
{
return replacestr;
}
if (datetimestr.Equals(""))
{
return replacestr;
}
try
{
datetimestr = Convert.ToDateTime(datetimestr).ToString("yyyy-MM-dd").Replace("1900-01-01", replacestr);
}
catch
{
return replacestr;
}
return datetimestr;
}
#endregion
#region 返回标准时间格式string
///
/// 返回标准时间格式string:yyyy-MM-dd HH:mm:ss
///
public static string GetDateTime()
{
return DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
}
#endregion
#region 获取文件内容
///
/// 获取文件内容
///
/// 文件路径
/// 文件内容
public static string GetFileContent(string strPath)
{
StreamReader fileStream = new StreamReader(strPath, Encoding.Default);
string ct = fileStream.ReadToEnd();
fileStream.Close();
return ct;
}
#endregion
#region 获得当前绝对路径
///
/// 获得当前绝对路径
///
/// 指定的路径
/// 绝对路径
public static string GetSitePath(string strPath)
{
if (HttpContext.Current != null)
{
return HttpContext.Current.Server.MapPath(strPath);
}
return Path.Combine(AppDomain.CurrentDomain.BaseDirectory, strPath);
}
#endregion
#region 获取字符串长度,注意,1个汉字长度为2
///
/// 获取字符串长度,注意,1个汉字长度为2
///
/// 要检查字符串
/// 字符串长度
public static int GetStringLength(string str)
{
return Encoding.Default.GetBytes(str).Length;
}
#endregion
#region MD5函数
///
/// MD5函数
///
/// 原始字符串
/// MD5结果
public static string MD5(string str)
{
byte[] b = Encoding.Default.GetBytes(str);
b = new MD5CryptoServiceProvider().ComputeHash(b);
string ret = "";
for (int i = 0; i < b.Length; i++)
{
ret = ret + b[i].ToString("x").PadLeft(2, '0');
}
return ret;
}
#endregion
#region 移除Html标记
///
/// 移除Html标记
///
///
///
public static string RemoveHtml(string content)
{
string regexstr = "<[^>]*>";
return Regex.Replace(content, regexstr, string.Empty, RegexOptions.IgnoreCase);
}
#endregion
#region 删除字符串尾部的回车/换行/空格
///
/// 删除字符串尾部的回车/换行/空格
///
///
///
public static string RTrim(string str)
{
for (int i = str.Length; i >= 0; i--)
{
char ts = str[i];
if (!ts.Equals(" "))
{
ts = str[i];
}
if (ts.Equals("\r") || (ts = str[i]).Equals("\n"))
{
str.Remove(i, 1);
}
}
return str;
}
#endregion
#region SHA256函数
///
/// SHA256函数
///
/// /// 原始字符串
/// SHA256结果
public static string SHA256(string str)
{
byte[] SHA256Data = Encoding.UTF8.GetBytes(str);
SHA256Managed Sha256 = new SHA256Managed();
byte[] Result = Sha256.ComputeHash(SHA256Data);
return Convert.ToBase64String(Result); //返回长度为44字节的字符串
}
#endregion
#region 生成指定数量的html空格符号
///
/// 生成指定数量的html空格符号
///
public static string Spaces(int nSpaces)
{
StringBuilder sb = new StringBuilder();
for (int i = 0; i < nSpaces; i++)
{
sb.Append(" ");
}
return sb.ToString();
}
#endregion
#region 生成指定数量的html符号
///
/// 生成指定数量的html符号
///
public static string Spaces(int nSpaces, string strHtml)
{
StringBuilder sb = new StringBuilder();
for (int i = 0; i < nSpaces; i++)
{
sb.Append(strHtml);
}
return sb.ToString();
}
#endregion
#region 替换回车换行符为html换行符
///
/// 替换回车换行符为html换行符
///
public static string StrFormat(string str)
{
string str2;
if (str == null)
{
str2 = "";
}
else
{
str = str.Replace("\r\n", "
");
str = str.Replace("\n", "
");
str2 = str;
}
return str2;
}
#endregion
#region string型转换为int型
///
/// string型转换为int型
///
/// 要转换的字符串
/// 缺省值
/// 转换后的int类型结果
public static int StrToInt(object strValue, int defValue)
{
if ((strValue == null) || (strValue.ToString() == string.Empty) || (strValue.ToString().Length > 10))
{
return defValue;
}
string val = strValue.ToString();
string firstletter = val[0].ToString();
if (val.Length == 10 && RegExp.IsNumber(firstletter) && int.Parse(firstletter) > 1)
{
return defValue;
}
else if (val.Length == 10 && !RegExp.IsNumber(firstletter))
{
return defValue;
}
int intValue = defValue;
if (strValue != null)
{
bool IsInt = new Regex(@"^([-]|[0-9])[0-9]*$").IsMatch(strValue.ToString());
if (IsInt)
{
intValue = Convert.ToInt32(strValue);
}
}
return intValue;
}
#endregion
#region 返回 URL 字符串的编码结果
///
/// 返回 URL 字符串的编码结果
///
/// 字符串
/// 解码结果
public static string UrlDecode(string str)
{
return HttpUtility.UrlDecode(str);
}
#endregion
#region 返回 HTML 字符串的编码结果
///
/// 返回 HTML 字符串的编码结果
///
/// 字符串
/// 编码结果
public static string HtmlEncode(string str)
{
return HttpUtility.HtmlEncode(str);
}
#endregion
#region 返回 HTML 字符串的解码结果
///
/// 返回 HTML 字符串的解码结果
///
/// 字符串
/// 解码结果
public static string HtmlDecode(string str)
{
return HttpUtility.HtmlDecode(str);
}
#endregion
}
}