1、Javascript类:
using System.Web;
namespace Common.Utility
{
/// <summary>
/// 客户端脚本输出
/// </summary>
public class JsHelper
{
/// <summary>
/// 弹出信息,并跳转指定页面。
/// </summary>
public static void AlertAndRedirect(string message, string toURL)
{
string js = "<script language=javascript>alert('{0}');window.location.replace('{1}')</script>";
HttpContext.Current.Response.Write(string.Format(js, message, toURL));
HttpContext.Current.Response.End();
}
/// <summary>
/// 弹出信息,并返回历史页面
/// </summary>
public static void AlertAndGoHistory(string message, int value)
{
string js = @"<Script language='JavaScript'>alert('{0}');history.go({1});</Script>";
HttpContext.Current.Response.Write(string.Format(js, message, value));
HttpContext.Current.Response.End();
}
/// <summary>
/// 直接跳转到指定的页面
/// </summary>
public static void Redirect(string toUrl)
{
string js = @"<script language=javascript>window.location.replace('{0}')</script>";
HttpContext.Current.Response.Write(string.Format(js, toUrl));
}
/// <summary>
/// 弹出信息 并指定到父窗口
/// </summary>
public static void AlertAndParentUrl(string message, string toURL)
{
string js = "<script language=javascript>alert('{0}');window.top.location.replace('{1}')</script>";
HttpContext.Current.Response.Write(string.Format(js, message, toURL));
}
/// <summary>
/// 返回到父窗口
/// </summary>
public static void ParentRedirect(string ToUrl)
{
string js = "<script language=javascript>window.top.location.replace('{0}')</script>";
HttpContext.Current.Response.Write(string.Format(js, ToUrl));
}
/// <summary>
/// 返回历史页面
/// </summary>
public static void BackHistory(int value)
{
string js = @"<Script language='JavaScript'>history.go({0});</Script>";
HttpContext.Current.Response.Write(string.Format(js, value));
HttpContext.Current.Response.End();
}
/// <summary>
/// 弹出信息
/// </summary>
public static void Alert(string message)
{
string js = "<script language=javascript>alert('{0}');</script>";
HttpContext.Current.Response.Write(string.Format(js, message));
}
/// <summary>
/// 注册脚本块
/// </summary>
public static void RegisterScriptBlock(System.Web.UI.Page page, string _ScriptString)
{
page.ClientScript.RegisterStartupScript(page.GetType(), "scriptblock", "<script type='text/javascript'>" + _ScriptString + "</script>");
}
}
}
2、二维码操作类:
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using ThoughtWorks.QRCode.Codec;
using ThoughtWorks.QRCode.Codec.Util;
using System.Web;
namespace Common.Utility
{
public class QRcode
{
/// <summary>
/// 生成二维码图片
/// </summary>
/// <param name="str">字符串</param>
/// <param name="size">图片大小</param>
/// <param name="path">图片位置
/// 例如 /abc/abc/
/// </param>
/// <returns>返回生成的二维码图片路径</returns>
public string Create(string str, int size, string path)
{
try
{
QRCodeEncoder qrCodeEncoder = new QRCodeEncoder();
//设置编码模式
qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE;
//设置编码测量度
qrCodeEncoder.QRCodeScale = size;
//设置编码版本
qrCodeEncoder.QRCodeVersion = 8;
//设置编码错误纠正
qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.M;
System.Drawing.Image image = qrCodeEncoder.Encode(str);
string filename = "~" + path + Guid.NewGuid() + ".jpg";
image.Save(HttpContext.Current.Request.MapPath(filename));
return filename.Replace("~", "");
}
catch (Exception)
{
return "";
}
}
/// <summary>
/// 调用此函数后使此两种图片合并,类似相册,有个
/// 背景图,中间贴自己的目标图片
/// </summary>
/// <param name="imgBack">粘贴的源图片</param>
/// <param name="destImg">粘贴的目标图片</param>
public static Image CombinImage(Image imgBack, string destImg)
{
Image img = Image.FromFile(destImg); //照片图片
if (img.Height != 65 || img.Width != 65)
{
img = KiResizeImage(img, 65, 65, 0);
}
Graphics g = Graphics.FromImage(imgBack);
g.DrawImage(imgBack, 0, 0, imgBack.Width, imgBack.Height); //g.DrawImage(imgBack, 0, 0, 相框宽, 相框高);
//g.FillRectangle(System.Drawing.Brushes.White, imgBack.Width / 2 - img.Width / 2 - 1, imgBack.Width / 2 - img.Width / 2 - 1,1,1);//相片四周刷一层黑色边框
//g.DrawImage(img, 照片与相框的左边距, 照片与相框的上边距, 照片宽, 照片高);
g.DrawImage(img, imgBack.Width / 2 - img.Width / 2, imgBack.Width / 2 - img.Width / 2, img.Width, img.Height);
GC.Collect();
return imgBack;
}
/// <summary>
/// Resize图片
/// </summary>
/// <param name="bmp">原始Bitmap</param>
/// <param name="newW">新的宽度</param>
/// <param name="newH">新的高度</param>
/// <param name="Mode">保留着,暂时未用</param>
/// <returns>处理以后的图片</returns>
public static Image KiResizeImage(Image bmp, int newW, int newH, int Mode)
{
try
{
Image b = new Bitmap(newW, newH);
Graphics g = Graphics.FromImage(b);
// 插值算法的质量
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.DrawImage(bmp, new Rectangle(0, 0, newW, newH), new Rectangle(0, 0, bmp.Width, bmp.Height), GraphicsUnit.Pixel);
g.Dispose();
return b;
}
catch
{
return null;
}
}
}
}
/*
* 引用ThoughtWorks.QRCode.dll
QRcode qrCode = new QRcode();
UserQrCode = qrCode.Create("http://www.kuiyu.net/" , 4, "/Content/Images/QrCode/");
*/
3、分词辅助类
using System;
using System.Collections;
using System.IO;
using System.Text.RegularExpressions;
namespace Utilities
{
/// <summary>
/// 分词辅助类
/// </summary>
public class SegList
{
public int MaxLength;
private ArrayList m_seg;
public int Count
{
get
{
return m_seg.Count;
}
}
public SegList()
{
m_seg = new ArrayList();
MaxLength = 0;
}
public void Add(object obj)
{
m_seg.Add(obj);
if (MaxLength < obj.ToString().Length)
{
MaxLength = obj.ToString().Length;
}
}
public object GetElem(int i)
{
if (i < this.Count)
return m_seg[i];
else
return null;
}
public void SetElem(int i, object obj)
{
m_seg[i] = obj;
}
public bool Contains(object obj)
{
return m_seg.Contains(obj);
}
/// <summary>
/// 按长度排序
/// </summary>
public void Sort()
{
Sort(this);
}
/// <summary>
/// 按长度排序
/// </summary>
public void Sort(SegList list)
{
int max = 0;
for (int i = 0; i < list.Count - 1; ++i)
{
max = i;
for (int j = i + 1; j < list.Count; ++j)
{
string str1 = list.GetElem(j).ToString();
string str2 = list.GetElem(max).ToString();
int l1;
int l2;
if (str1 == "null")
l1 = 0;
else
l1 = str1.Length;
if (str2 == "null")
l2 = 0;
else
l2 = str2.Length;
if (l1 > l2)
max = j;
}
object o = list.GetElem(max);
list.SetElem(max, list.GetElem(i));
list.SetElem(i, o);
}
}
}
/// <summary>
/// 分词类
/// </summary>
//----------------调用----------------------
//Segment seg = new Segment();
//seg.InitWordDics();
//seg.EnablePrefix = true;
//seg.Separator =" ";
//seg.SegmentText("字符串", false).Trim();
//-------------------------------------------
public class Segment
{
#region 私有字段
private string m_DicPath = System.Web.HttpContext.Current.Server.MapPath("bin/ShootSeg/sDict.dic");
private string m_NoisePath = System.Web.HttpContext.Current.Server.MapPath("bin/ShootSeg/sNoise.dic");
private string m_NumberPath = System.Web.HttpContext.Current.Server.MapPath("bin/ShootSeg/sNumber.dic");
private string m_WordPath = System.Web.HttpContext.Current.Server.MapPath("bin/ShootSeg/sWord.dic");
private string m_PrefixPath = System.Web.HttpContext.Current.Server.MapPath("bin/ShootSeg/sPrefix.dic");
private Hashtable htWords;
private ArrayList alNoise;
private ArrayList alNumber;
private ArrayList alWord;
private ArrayList alPrefix;
private double m_EventTime = 0;
/// <summary>
/// 分隔符
/// </summary>
private string m_Separator = " ";
/// <summary>
/// 用于验证汉字的正则表达式
/// </summary>
private string strChinese = "[\u4e00-\u9fa5]";
#endregion
#region 公有属性
/// <summary>
/// 基本词典路径
/// </summary>
public string DicPath
{
get
{
return m_DicPath;
}
set
{
m_DicPath = value;
}
}
/// <summary>
/// 数据缓存函数
/// </summary>
/// <param name="key">索引键</param>
/// <param name="val">缓存的数据</param>
private static void SetCache(string key, object val)
{
if (val == null) val = " ";
System.Web.HttpContext.Current.Application.Lock();
System.Web.HttpContext.Current.Application.Set(key, val);
System.Web.HttpContext.Current.Application.UnLock();
}
/// <summary>
/// 读取缓存
/// </summary>
private static object GetCache(string key)
{
return System.Web.HttpContext.Current.Application.Get(key);
}
/// <summary>
/// 暂时无用
/// </summary>
public string NoisePath
{
get
{
return m_NoisePath;
}
set
{
m_NoisePath = value;
}
}
/// <summary>
/// 数字词典路径
/// </summary>
public string NumberPath
{
get
{
return m_NumberPath;
}
set
{
m_NumberPath = value;
}
}
/// <summary>
/// 字母词典路径
/// </summary>
public string WordPath
{
get
{
return m_WordPath;
}
set
{
m_WordPath = value;
}
}
/// <summary>
/// 姓名前缀字典 用于纠错姓名
/// </summary>
public string PrefixPath
{
get
{
return m_PrefixPath;
}
set
{
m_PrefixPath = value;
}
}
/// <summary>
/// 是否开启姓名纠错功能
/// </summary>
public bool EnablePrefix
{
get
{
if (alPrefix.Count == 0)
return false;
else
return true;
}
set
{
if (value)
alPrefix = LoadWords(PrefixPath, alPrefix);
else
alPrefix = new ArrayList();
}
}
/// <summary>
/// 用时每次进行加载或分词动作后改属性表示为上一次动作所用时间
/// 已精确到毫秒但分词操作在字符串较短时可能为0
/// </summary>
public double EventTime
{
get
{
return m_EventTime;
}
}
/// <summary>
/// 分隔符,默认为空格
/// </summary>
public string Separator
{
get
{
return m_Separator;
}
set
{
if (value != "" && value != null) m_Separator = value;
}
}
#endregion
#region 构造方法
/// <summary>
/// 构造方法
/// </summary>
public Segment()
{ }
/// <summary>
/// 构造方法
/// </summary>
public Segment(string p_DicPath, string p_NoisePath, string p_NumberPath, string p_WordPath)
{
m_WordPath = p_DicPath;
m_WordPath = p_NoisePath;
m_WordPath = p_NumberPath;
m_WordPath = p_WordPath;
this.InitWordDics();
}
#endregion
#region 公有方法
/// <summary>
/// 加载词列表
/// </summary>
public void InitWordDics()
{
DateTime start = DateTime.Now;
if (GetCache("jcms_dict") == null)
{
htWords = new Hashtable();
Hashtable father = htWords;
Hashtable forfather = htWords;
string strChar1;
string strChar2;
StreamReader reader = new StreamReader(DicPath, System.Text.Encoding.UTF8);
string strline = reader.ReadLine();
SegList list;
Hashtable child = new Hashtable();
long i = 0;
while (strline != null && strline.Trim() != "")
{
i++;
strChar1 = strline.Substring(0, 1);
strChar2 = strline.Substring(1, 1);
if (!htWords.ContainsKey(strChar1))
{
father = new Hashtable();
htWords.Add(strChar1, father);
}
else
{
father = (Hashtable)htWords[strChar1];
}
if (!father.ContainsKey(strChar2))
{
list = new SegList();
if (strline.Length > 2)
list.Add(strline.Substring(2));
else
list.Add("null");
father.Add(strChar2, list);
}
else
{
list = (SegList)father[strChar2];
if (strline.Length > 2)
{
list.Add(strline.Substring(2));
}
else
{
list.Add("null");
}
father[strChar2] = list;
}
htWords[strChar1] = father;
strline = reader.ReadLine();
}
try
{
reader.Close();
}
catch
{ }
SetCache("jcms_dict", htWords);
}
htWords = (Hashtable)GetCache("jcms_dict");
alNoise = LoadWords(NoisePath, alNoise);
alNumber = LoadWords(NumberPath, alNumber);
alWord = LoadWords(WordPath, alWord);
alPrefix = LoadWords(PrefixPath, alPrefix);
TimeSpan duration = DateTime.Now - start;
m_EventTime = duration.TotalMilliseconds;
}
/// <summary>
/// 加载文本词组到ArrayList
/// </summary>
public ArrayList LoadWords(string strPath, ArrayList list)
{
StreamReader reader = new StreamReader(strPath, System.Text.Encoding.UTF8);
list = new ArrayList();
string strline = reader.ReadLine();
while (strline != null)
{
list.Add(strline);
strline = reader.ReadLine();
}
try
{
reader.Close();
}
catch
{ }
return list;
}
/// <summary>
/// 输出词列表
/// </summary>
public void OutWords()
{
IDictionaryEnumerator idEnumerator1 = htWords.GetEnumerator();
while (idEnumerator1.MoveNext())
{
IDictionaryEnumerator idEnumerator2 = ((Hashtable)idEnumerator1.Value).GetEnumerator();
while (idEnumerator2.MoveNext())
{
SegList aa = (SegList)idEnumerator2.Value;
for (int i = 0; i < aa.Count; i++)
{
Console.WriteLine(idEnumerator1.Key.ToString() + idEnumerator2.Key.ToString() + aa.GetElem(i).ToString());
}
}
}
}
/// <summary>
/// 输出ArrayList
/// </summary>
public void OutArrayList(ArrayList list)
{
if (list == null) return;
for (int i = 0; i < list.Count; i++)
{
Console.WriteLine(list[i].ToString());
}
}
/// <summary>
/// 分词过程,不支持回车
/// </summary>
/// <param name="strText">要分词的文本</param>
/// <returns>分词后的文本</returns>
public string SegmentText(string strText)
{
strText = (strText + "$").Trim();
if (htWords == null) return strText;
if (strText.Length < 3) return strText;
DateTime start = DateTime.Now;
int length = 0;
int preFix = 0;
bool word = false;
bool number = false;
string reText = "";
string strPrefix = "";
string strLastChar = "";
string strLastWords = Separator;
for (int i = 0; i < strText.Length - 1; i++)
{
#region 对于每一个字的处理过程
string strChar1 = strText.Substring(i, 1);
string strChar2 = strText.Substring(i + 1, 1).Trim();
bool yes;
SegList l;
Hashtable h;
if (reText.Length > 0) strLastChar = reText.Substring(reText.Length - 1);
if (strChar1 == " ")
{
if ((number || word) && strLastChar != Separator) reText += this.Separator;
yes = true;
}
else
yes = false;
int CharType = GetCharType(strChar1);
switch (CharType)
{
case 1:
#region 如果是数字,如果数字的上一位是字母要和后面的数字分开
if (word)
{
reText += Separator;
}
word = false;
number = true;
strLastWords = "";
break;
#endregion
case 2:
case 5:
#region 如果是字母
if (number)
strLastWords = Separator;
else
strLastWords = "";
word = true;
number = false;
break;
#endregion
case 3:
case 4:
#region 第一级哈希表是否包含关键字,假如包含处理第二级哈希表
//上一个字是否为字母
if (word) reText += Separator;
#region 检测上一个是否是数字,这个过程是用于修正数字后的量词的
if (number && CharType != 4)
{
h = (Hashtable)htWords["n"];
if (h.ContainsKey(strChar1))
{
l = (SegList)h[strChar1];
if (l.Contains(strChar2))
{
reText += strChar1 + strChar2 + Separator;
yes = true;
i++;
}
else if (l.Contains("null"))
{
reText += strChar1 + Separator;
yes = true;
}
}
else
reText += Separator;
}
#endregion
//非汉字数字的汉字
if (CharType == 3)
{
word = false;
number = false;
strLastWords = Separator;
}
else
{
word = false;
number = true;
strLastWords = "";
}
//第二级哈希表取出
h = (Hashtable)htWords[strChar1];
//第二级哈希表是否包含关键字
if (h.ContainsKey(strChar2))
{
#region 第二级包含关键字
//取出ArrayList对象
l = (SegList)h[strChar2];
//遍历每一个对象 看是否能组合成词
for (int j = 0; j < l.Count; j++)
{
bool have = false;
string strChar3 = l.GetElem(j).ToString();
//对于每一个取出的词进行检测,看是否匹配,长度保护
if ((strChar3.Length + i + 2) < strText.Length)
{
//向i+2后取出m长度的字
string strChar = strText.Substring(i + 2, strChar3.Length).Trim();
if (strChar3 == strChar && !yes)
{
if (strPrefix != "")
{
reText += strPrefix + Separator;
strPrefix = "";
preFix = 0;
}
reText += strChar1 + strChar2 + strChar;
i += strChar3.Length + 1;
have = true;
yes = true;
break;
}
}
else if ((strChar3.Length + i + 2) == strText.Length)
{
string strChar = strText.Substring(i + 2).Trim();
if (strChar3 == strChar && !yes)
{
if (strPrefix != "")
{
reText += strPrefix + Separator;
strPrefix = "";
preFix = 0;
}
reText += strChar1 + strChar2 + strChar;
i += strChar3.Length + 1;
have = true;
yes = true;
break;
}
}
if (!have && j == l.Count - 1 && l.Contains("null") && !yes)
{
if (preFix == 1)
{
reText += strPrefix + strChar1 + strChar2;
strPrefix = "";
preFix = 0;
}
else if (preFix > 1)
{
reText += strPrefix + strLastWords + strChar1 + strChar2;
strPrefix = "";
preFix = 0;
}
else
{
if (CharType == 4) reText += strChar1 + strChar2;
else reText += strChar1 + strChar2;
strLastWords = this.Separator;
number = false;
}
i++;
yes = true;
break;
}
else if (have)
{
break;
}
}
#endregion
//如果没有匹配还可能有一种情况,这个词语只有两个字,以这两个字开头的词语不存在
if (!yes && l.Contains("null"))
{
if (preFix == 1)
{
reText += strPrefix + strChar1 + strChar2;
strPrefix = "";
preFix = 0;
}
else if (preFix > 1)
{
reText += strPrefix + strLastWords + strChar1 + strChar2;
strPrefix = "";
preFix = 0;
}
else
{
if (CharType == 4) reText += strChar1 + strChar2;
else reText += strChar1 + strChar2;
strLastWords = this.Separator;
number = false;
}
i++;
yes = true;
}
if (reText.Length > 0) strLastChar = reText.Substring(reText.Length - 1);
if (CharType == 4 && GetCharType(strLastChar) == 4)
{
number = true;
}
else if (strLastChar != this.Separator) reText += this.Separator;
}
#endregion
break;
default:
#region 未知字符,可能是生僻字,也可能是标点符合之类
if (word && !yes)
{
reText += Separator;
}
else if (number && !yes)
{
reText += Separator;
}
number = false;
word = false;
strLastWords = this.Separator;
break;
#endregion
}
if (!yes && number || !yes && word)
{
reText += strChar1;
yes = true;
}
if (!yes)
{
#region 处理姓名问题
if (preFix == 0)
{
if (alPrefix.Contains(strChar1 + strChar2))
{
i++;
strPrefix = strChar1 + strChar2;
preFix++;
}
else if (alPrefix.Contains(strChar1))
{
if (!number)
{
strPrefix = strChar1;
preFix++;
}
else
{
reText += strChar1 + strLastWords;
number = false;
word = false;
}
}
else
{
if (preFix == 3)
{
reText += strPrefix + Separator + strChar1 + Separator;
strPrefix = "";
preFix = 0;
}
else if (preFix > 0)
{
if (Regex.IsMatch(strChar1, strChinese))
{
strPrefix += strChar1;
preFix++;
}
else
{
reText += strPrefix + Separator + strChar1 + Separator;
strPrefix = "";
preFix = 0;
}
}
else
{
reText += strChar1 + strLastWords;
number = false;
word = false;
}
}
}
else
{
if (preFix == 3)
{
reText += strPrefix + Separator + strChar1 + Separator;
strPrefix = "";
preFix = 0;
}
else if (preFix > 0)
{
if (Regex.IsMatch(strChar1, strChinese))
{
strPrefix += strChar1;
preFix++;
}
else
{
reText += strPrefix + Separator + strChar1 + Separator;
strPrefix = "";
preFix = 0;
}
}
else
{
reText += strChar1 + strLastWords;
number = false;
}
}
#endregion
}
length = i;
#endregion
}
#region 最后防止最后一个字的丢失
if (length < strText.Length - 1)
{
string strLastChar1 = strText.Substring(strText.Length - 1).Trim();
string strLastChar2 = strText.Substring(strText.Length - 2).Trim();
if (reText.Length > 0) strLastChar = reText.Substring(reText.Length - 1);
if (preFix != 0)
{
reText += strPrefix + strLastChar1;
}
else
{
switch (GetCharType(strLastChar1))
{
case 1:
if (strLastChar1 != "." && strLastChar1 != ".")
reText += strLastChar1;
else
reText += Separator + strLastChar1;
break;
case 2:
case 5:
if (alWord.Contains(strLastChar2))
reText += strLastChar1;
break;
case 3:
case 4:
if ((number || word) && strLastChar != Separator)
reText += Separator + strLastChar1;
else
reText += strLastChar1;
break;
default:
if (strLastChar != Separator)
reText += Separator + strLastChar1;
else
reText += strLastChar1;
break;
}
}
if (reText.Length > 0) strLastChar = (reText.Substring(reText.Length - 1));
if (strLastChar != this.Separator) reText += this.Separator;
}
#endregion
TimeSpan duration = DateTime.Now - start;
m_EventTime = duration.TotalMilliseconds;
return reText.Replace(" $", ""); //这里包含一个字的,则去掉
}
/// <summary>
/// 重载分词过程,支持回车
/// </summary>
public string SegmentText(string strText, bool Enter)
{
if (Enter)
{
DateTime start = DateTime.Now;
string[] strArr = strText.Split('\n');
string reText = "";
for (int i = 0; i < strArr.Length; i++)
{
reText += SegmentText(strArr[i]) + "\r\n";
}
TimeSpan duration = DateTime.Now - start;
m_EventTime = duration.TotalMilliseconds;
return reText;
}
else
{
return SegmentText(strText);
}
}
#region 判断字符类型
/// <summary>
/// 判断字符类型,0为未知,1为数字,2为字母,3为汉字,4为汉字数字
/// </summary>
private int GetCharType(string p_Char)
{
int CharType = 0;
if (alNumber.Contains(p_Char)) CharType = 1;
if (alWord.Contains(p_Char)) CharType = 2;
if (htWords.ContainsKey(p_Char)) CharType += 3;
return CharType;
}
#endregion
#region 对加载的词典排序并重新写入
/// <summary>
/// 对加载的词典排序并重新写入
/// </summary>
public void SortDic()
{
SortDic(false);
}
/// <summary>
/// 对加载的词典排序并重新写入
/// </summary>
/// <param name="Reload">是否重新加载</param>
public void SortDic(bool Reload)
{
DateTime start = DateTime.Now;
StreamWriter sw = new StreamWriter(DicPath, false, System.Text.Encoding.UTF8);
IDictionaryEnumerator idEnumerator1 = htWords.GetEnumerator();
while (idEnumerator1.MoveNext())
{
IDictionaryEnumerator idEnumerator2 = ((Hashtable)idEnumerator1.Value).GetEnumerator();
while (idEnumerator2.MoveNext())
{
SegList aa = (SegList)idEnumerator2.Value;
aa.Sort();
for (int i = 0; i < aa.Count; i++)
{
if (aa.GetElem(i).ToString() == "null")
sw.WriteLine(idEnumerator1.Key.ToString() + idEnumerator2.Key.ToString());
else
sw.WriteLine(idEnumerator1.Key.ToString() + idEnumerator2.Key.ToString() + aa.GetElem(i).ToString());
}
}
}
sw.Close();
if (Reload) InitWordDics();
TimeSpan duration = DateTime.Now - start;
m_EventTime = duration.TotalMilliseconds;
}
#endregion
/// <summary>
/// 删除两行完全相同的词,暂时无用!
/// </summary>
/// <returns>相同词条个数</returns>
public int Optimize()
{
int l = 0;
DateTime start = DateTime.Now;
Hashtable htOptimize = new Hashtable();
StreamReader reader = new StreamReader(DicPath, System.Text.Encoding.UTF8);
string strline = reader.ReadLine();
while (strline != null && strline.Trim() != "")
{
if (!htOptimize.ContainsKey(strline))
htOptimize.Add(strline, null);
else
l++;
}
Console.WriteLine("ready");
try
{
reader.Close();
}
catch { }
StreamWriter sw = new StreamWriter(DicPath, false, System.Text.Encoding.UTF8);
IDictionaryEnumerator ide = htOptimize.GetEnumerator();
while (ide.MoveNext())
sw.WriteLine(ide.Key.ToString());
try
{
sw.Close();
}
catch { }
TimeSpan duration = DateTime.Now - start;
m_EventTime = duration.TotalMilliseconds;
return l;
}
#endregion
}
}
4、分页类:
using System;
using System.Text;
using System.Web;
using System.Web.UI.WebControls;
using System.Text.RegularExpressions;
namespace Utilities
{
public class PageValidate
{
#region 分页
public static string paging(string url, string para, int sumpage, int page)
{
string result = string.Empty;
if (sumpage == 1)
{
return result;
}
if (sumpage > 500)
{
sumpage = 500;
}
if (page > sumpage)
{
page = 1;
}
StringBuilder sb = new StringBuilder();
if (sumpage > 0)
{
switch (page)
{
case 1:
sb.Append(string.Format("<p class=\"next\"><a href=\"{0}?page={1}{2}\">{3}</a> ", new object[] { url, page + 1, para, "下一页" }));
break;
default:
if (sumpage == page)
{
sb.Append(string.Format("<p class=\"next\"><a href=\"{0}?page={1}{2}\">{3}</a> ", new object[] { url, page - 1, para, "上一页" }));
}
else
{
sb.Append(string.Format("<p class=\"next\"><a href=\"{0}?page={1}{2}\">{3}</a> <a href=\"{4}?page={5}{6}\">{7}</a> ",
new object[] { url, page + 1, para, "下一页", url, page - 1, para, "上一页" }));
}
break;
}
sb.Append(string.Format("第{0}/{1}页</p>", new object[] { page, sumpage }));
}
return sb.ToString();
}
public static string paging(string url, string para, int sumpage, int page, System.Web.UI.UserControl myPaging)
{
myPaging.Visible = false;
string result = string.Empty;
if (sumpage == 1)
{
return result;
}
if (sumpage > 500)
{
sumpage = 500;
}
if (page > sumpage)
{
page = 1;
}
StringBuilder sb = new StringBuilder();
if (sumpage > 0)
{
myPaging.Visible = true;
switch (page)
{
case 1:
sb.Append(string.Format("<a href=\"{0}?page={1}{2}\">{3}</a> ", new object[] { url, page + 1, para, "下一页" }));
break;
default:
if (sumpage == page)
{
sb.Append(string.Format("<a href=\"{0}?page={1}{2}\">{3}</a> ", new object[] { url, page - 1, para, "上一页" }));
}
else
{
sb.Append(string.Format("<a href=\"{0}?page={1}{2}\">{3}</a> <a href=\"{4}?page={5}{6}\">{7}</a> ",
new object[] { url, page + 1, para, "下一页", url, page - 1, para, "上一页" }));
}
break;
}
sb.Append(string.Format("第{0}/{1}页", new object[] { page, sumpage }));
}
return sb.ToString();
}
public static string paging(string para, int sumpage, int page, int count)
{
string result = string.Empty;
if (page > sumpage)
{
page = 1;
}
StringBuilder sb = new StringBuilder();
if (sumpage > 0)
{
if (sumpage != 1)
{
switch (page)
{
case 1:
sb.Append(string.Format("<a href=\"?page={0}{1}\">{2}</a> ", new object[] { page + 1, para, "下一页" }));
break;
default:
if (sumpage == page)
{
sb.Append(string.Format("<a href=\"?page={0}{1}\">{2}</a> ", new object[] { page - 1, para, "上一页" }));
}
else
{
sb.Append(string.Format("<a href=\"?page={0}{1}\">{2}</a> <a href=\"?page={3}{4}\">{5}</a> ",
new object[] { page - 1, para, "上一页", page + 1, para, "下一页" }));
}
break;
}
}
sb.Append(string.Format("第{0}/{1}页 共{2}条", new object[] { page, sumpage, count }));
}
return sb.ToString();
}
public static void paging(string clinktail, int sumpage, int page, System.Web.UI.WebControls.Label page_view)
{
if (sumpage > 0)
{
int n = sumpage; //总页数
int x = page; //得到当前页
int i;
int endpage;
string pageview = "", pageviewtop = "";
if (x > 1)
{
pageview += " <a class='pl' href='?page=1" + clinktail + "'>第1页</a> | ";
pageviewtop += " <a class='pl' href='?page=1" + clinktail + "'>第1页</a> | ";
}
else
{
pageview += " <font color='#666666'> 第1页 </font> | ";
pageviewtop += " <font color='#666666'> 第1页 </font> | ";
}
if (x > 1)
{
pageviewtop += " <a class='pl' href='?page=" + (x - 1) + "" + clinktail + "'>上1页</a> ";
}
else
{
pageviewtop += " <font color='#666666'>上1页</font> ";
}
if (x > ((x - 1) / 10) * 10 && x > 10)
{
pageview += "<a class='pl' href='?page=" + ((x - 1) / 10) * 10 + "" + clinktail + "' onclink='return false;'>上10页</a>";
}
//if (((x-1) / 10) * 10 + 10) >= n )
if (((x - 1) / 10) * 10 + 10 >= n)
{
endpage = n;
}
else
{
endpage = ((x - 1) / 10) * 10 + 10;
}
for (i = ((x - 1) / 10) * 10 + 1; i <= endpage; ++i)
{
if (i == x)
{
pageview += " <font color='#FF0000'><b>" + i + "</b></font>";
}
else
{
pageview += " <a class='pl' href='?page=" + i + "" + clinktail + "'>" + i + "</a>";
}
}
if (x < n)
{
pageviewtop += " <a class='pl' href='?page=" + (x + 1) + "" + clinktail + "'>下1页</a> ";
}
else
{
pageviewtop += " <font color='#666666'>下1页</font> ";
}
if (endpage != n)
{
pageview += " <a class='pl' href='?page=" + (endpage + 1) + "" + clinktail + "' class='pl' onclink='return false;'>下10页</a> | ";
}
else
{
pageview += " | ";
}
if (x < n)
{
pageview += " <a class='pl' href='?page=" + n + "" + clinktail + "' class='pl'>第" + n + "页</a> ";
pageviewtop += " | <a class='pl' href='?page=" + n + "" + clinktail + "' class='pl'>第" + n + "页</a> ";
}
else
{
pageview += "<font color='#666666'> 第" + n + "页 </font>";
pageviewtop += " | <font color='#666666'> 第" + n + "页 </font>";
}
page_view.Text = pageview.ToString();
}
else
{
page_view.Text = "";
}
}
//带第一页和最后一页
public static string paging2(string para, int sumpage, int page, int count)
{
string result = string.Empty;
if (page > sumpage)
{
page = 1;
}
StringBuilder sb = new StringBuilder();
if (sumpage > 0)
{
if (sumpage != 1)
{
//第一页
sb.Append(string.Format("<a href=\"?page={0}{1}\"><img src=\"images/first-icon.gif\" border=\"0\"/></a> ", new object[] { 1, para }));
switch (page)
{
case 1:
//前一页图片
sb.Append(string.Format("<a>{0}</a>", new object[] { "<img src=\"images/left-icon.gif\" border=\"0\"/>" }));
sb.Append(string.Format("<a>上一页</a><a href=\"?page={0}{1}\">{2}</a> ", new object[] { page + 1, para, "下一页" }));
//后一页图片
sb.Append(string.Format("<a href=\"?page={0}{1}\">{2}</a>", new object[] { page + 1, para, "<img src=\"images/right-icon.gif\" border=\"0\"/>" }));
break;
default:
if (sumpage == page)
{
//前一页图片
sb.Append(string.Format("<a href=\"?page={0}{1}\">{2}</a>", new object[] { page - 1, para, "<img src=\"images/left-icon.gif\" border=\"0\"/>" }));
sb.Append(string.Format("<a href=\"?page={0}{1}\">{2}</a><a>下一页</a> ", new object[] { page - 1, para, "上一页" }));
//后一页图片
sb.Append(string.Format("<a>{0}</a>", new object[] { "<img src=\"images/right-icon.gif\" />" }));
}
else
{
//前一页图片
sb.Append(string.Format("<a href=\"?page={0}{1}\">{2}</a>", new object[] { page - 1, para, "<img src=\"images/left-icon.gif\" border=\"0\"/>" }));
sb.Append(string.Format("<a href=\"?page={0}{1}\">{2}</a> <a href=\"?page={3}{4}\">{5}</a> ",
new object[] { page - 1, para, "上一页", page + 1, para, "下一页" }));
//后一页图片
sb.Append(string.Format("<a href=\"?page={0}{1}\">{2}</a>", new object[] { page + 1, para, "<img src=\"images/right-icon.gif\" border=\"0\"/>" }));
}
break;
}
//最后一页图片
sb.Append(string.Format(" <a href=\"?page={0}{1}\"><img src=\"images/last-icon.gif\" border=\"0\"/></a> ", new object[] { sumpage, para }));
}
sb.Append(string.Format("第{0}页/共{1}页 共{2}条", new object[] { page, sumpage, count }));
}
return sb.ToString();
}
public static string paging3(string url, string para, int sumpage, int page, int count)
{
string result = string.Empty;
if (page > sumpage)
{
page = 1;
}
StringBuilder sb = new StringBuilder();
if (sumpage > 0)
{
if (sumpage != 1)
{
//第一页
sb.Append(string.Format("<a href=\"{2}?page={0}{1}\">首页</a>", new object[] { 1, para, url }));
switch (page)
{
case 1:
//前一页图片
// sb.Append(string.Format("<a>{0}</a>", new object[] { "<img src=\"images/left-icon.gif\" border=\"0\"/>" }));
sb.Append(string.Format("<a>上一页</a><a href=\"{3}?page={0}{1}\">{2}</a> ", new object[] { page + 1, para, "下一页", url }));
//后一页图片
// sb.Append(string.Format("<a href=\"?page={0}{1}\">{2}</a>", new object[] { page + 1, para, "<img src=\"images/right-icon.gif\" border=\"0\"/>" }));
break;
default:
if (sumpage == page)
{
//前一页图片
//sb.Append(string.Format("<a href=\"?page={0}{1}\">{2}</a>", new object[] { page - 1, para, "<img src=\"images/left-icon.gif\" border=\"0\"/>" }));
sb.Append(string.Format("<a href=\"{3}?page={0}{1}\">{2}</a><a>下一页</a> ", new object[] { page - 1, para, "上一页", url }));
//后一页图片
//sb.Append(string.Format("<a>{0}</a>", new object[] { "<img src=\"images/right-icon.gif\" />" }));
}
else
{
//前一页图片
//sb.Append(string.Format("<a href=\"?page={0}{1}\">{2}</a>", new object[] { page - 1, para, "<img src=\"images/left-icon.gif\" border=\"0\"/>" }));
sb.Append(string.Format("<a href=\"{6}?page={0}{1}\">{2}</a> <a href=\"{6}?page={3}{4}\">{5}</a> ",
new object[] { page - 1, para, "上一页", page + 1, para, "下一页", url }));
//后一页图片
//sb.Append(string.Format("<a href=\"?page={0}{1}\">{2}</a>", new object[] { page + 1, para, "<img src=\"images/right-icon.gif\" border=\"0\"/>" }));
}
break;
}
//最后一页图片
sb.Append(string.Format("<a href=\"{2}?page={0}{1}\">末页</a> ", new object[] { sumpage, para, url }));
}
sb.Append(string.Format("第{0}页/共{1}页 共{2}条", new object[] { page, sumpage, count }));
}
return sb.ToString();
}
#endregion
}
}
5、验证类
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using System.Text.RegularExpressions;
namespace HD.Helper.Common
{
/// <summary>
/// 各种验证帮助类
/// </summary>
public class ValidatorHelper
{
#region 验证邮箱
/// <summary>
/// 验证邮箱
/// </summary>
/// <param name="source"></param>
/// <returns></returns>
public static bool IsEmail(string source)
{
return Regex.IsMatch(source, @"^[A-Za-z0-9](([_\.\-]?[a-zA-Z0-9]+)*)@([A-Za-z0-9]+)(([\.\-]?[a-zA-Z0-9]+)*)\.([A-Za-z]{2,})$", RegexOptions.IgnoreCase);
}
public static bool HasEmail(string source)
{
return Regex.IsMatch(source, @"[A-Za-z0-9](([_\.\-]?[a-zA-Z0-9]+)*)@([A-Za-z0-9]+)(([\.\-]?[a-zA-Z0-9]+)*)\.([A-Za-z]{2,})", RegexOptions.IgnoreCase);
}
#endregion
#region 验证网址
/// <summary>
/// 验证网址
/// </summary>
/// <param name="source"></param>
/// <returns></returns>
public static bool IsUrl(string source)
{
return Regex.IsMatch(source, @"^(((file|gopher|news|nntp|telnet|http|ftp|https|ftps|sftp)://)|(www\.))+(([a-zA-Z0-9\._-]+\.[a-zA-Z]{2,6})|([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}))(/[a-zA-Z0-9\&%_\./-~-]*)?$", RegexOptions.IgnoreCase);
}
public static bool HasUrl(string source)
{
return Regex.IsMatch(source, @"(((file|gopher|news|nntp|telnet|http|ftp|https|ftps|sftp)://)|(www\.))+(([a-zA-Z0-9\._-]+\.[a-zA-Z]{2,6})|([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}))(/[a-zA-Z0-9\&%_\./-~-]*)?", RegexOptions.IgnoreCase);
}
#endregion
#region 验证日期
/// <summary>
/// 验证日期
/// </summary>
/// <param name="source"></param>
/// <returns></returns>
public static bool IsDateTime(string source)
{
try
{
DateTime time = Convert.ToDateTime(source);
return true;
}
catch
{
return false;
}
}
#endregion
#region 验证手机号
/// <summary>
/// 验证手机号
/// </summary>
/// <param name="source"></param>
/// <returns></returns>
public static bool IsMobile(string source)
{
return Regex.IsMatch(source, @"^1[35]\d{9}$", RegexOptions.IgnoreCase);
}
public static bool HasMobile(string source)
{
return Regex.IsMatch(source, @"1[35]\d{9}", RegexOptions.IgnoreCase);
}
#endregion
#region 验证IP
/// <summary>
/// 验证IP
/// </summary>
/// <param name="source"></param>
/// <returns></returns>
public static bool IsIP(string source)
{
return Regex.IsMatch(source, @"^(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])$", RegexOptions.IgnoreCase);
}
public static bool HasIP(string source)
{
return Regex.IsMatch(source, @"(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])", RegexOptions.IgnoreCase);
}
public static bool IsIp(string ip)
{
bool result = false;
try
{
string[] iparg = ip.Split('.');
if (string.Empty != ip && ip.Length < 16 && iparg.Length == 4)
{
int intip;
for (int i = 0; i < 4; i++)
{
intip = Convert.ToInt16(iparg[i]);
if (intip > 255)
{
result = false;
return result;
}
}
result = true;
}
}
catch
{
return result;
}
return result;
}
#endregion
#region 验证身份证是否有效
/// <summary>
/// 验证身份证是否有效
/// </summary>
/// <param name="Id"></param>
/// <returns></returns>
public static bool IsIDCard(string Id)
{
if (Id.Length == 18)
{
bool check = IsIDCard18(Id);
return check;
}
else if (Id.Length == 15)
{
bool check = IsIDCard15(Id);
return check;
}
else
{
return false;
}
}
public static bool IsIDCard18(string Id)
{
long n = 0;
if (long.TryParse(Id.Remove(17), out n) == false || n < Math.Pow(10, 16) || long.TryParse(Id.Replace('x', '0').Replace('X', '0'), out n) == false)
{
return false;//数字验证
}
string address = "11x22x35x44x53x12x23x36x45x54x13x31x37x46x61x14x32x41x50x62x15x33x42x51x63x21x34x43x52x64x65x71x81x82x91";
if (address.IndexOf(Id.Remove(2)) == -1)
{
return false;//省份验证
}
string birth = Id.Substring(6, 8).Insert(6, "-").Insert(4, "-");
DateTime time = new DateTime();
if (DateTime.TryParse(birth, out time) == false)
{
return false;//生日验证
}
string[] arrVarifyCode = ("1,0,x,9,8,7,6,5,4,3,2").Split(',');
string[] Wi = ("7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2").Split(',');
char[] Ai = Id.Remove(17).ToCharArray();
int sum = 0;
for (int i = 0; i < 17; i++)
{
sum += int.Parse(Wi[i]) * int.Parse(Ai[i].ToString());
}
int y = -1;
Math.DivRem(sum, 11, out y);
if (arrVarifyCode[y] != Id.Substring(17, 1).ToLower())
{
return false;//校验码验证
}
return true;//符合GB11643-1999标准
}
public static bool IsIDCard15(string Id)
{
long n = 0;
if (long.TryParse(Id, out n) == false || n < Math.Pow(10, 14))
{
return false;//数字验证
}
string address = "11x22x35x44x53x12x23x36x45x54x13x31x37x46x61x14x32x41x50x62x15x33x42x51x63x21x34x43x52x64x65x71x81x82x91";
if (address.IndexOf(Id.Remove(2)) == -1)
{
return false;//省份验证
}
string birth = Id.Substring(6, 6).Insert(4, "-").Insert(2, "-");
DateTime time = new DateTime();
if (DateTime.TryParse(birth, out time) == false)
{
return false;//生日验证
}
return true;//符合15位身份证标准
}
#endregion
#region 是不是Int型的
/// <summary>
/// 是不是Int型的
/// </summary>
/// <param name="source"></param>
/// <returns></returns>
public static bool IsInt(string source)
{
Regex regex = new Regex(@"^(-){0,1}\d+$");
if (regex.Match(source).Success)
{
if ((long.Parse(source) > 0x7fffffffL) || (long.Parse(source) < -2147483648L))
{
return false;
}
return true;
}
return false;
}
#endregion
#region 看字符串的长度是不是在限定数之间 一个中文为两个字符
/// <summary>
/// 看字符串的长度是不是在限定数之间 一个中文为两个字符
/// </summary>
/// <param name="source">字符串</param>
/// <param name="begin">大于等于</param>
/// <param name="end">小于等于</param>
/// <returns></returns>
public static bool IsLengthStr(string source, int begin, int end)
{
int length = Regex.Replace(source, @"[^\x00-\xff]", "OK").Length;
if ((length <= begin) && (length >= end))
{
return false;
}
return true;
}
#endregion
#region 是不是中国电话,格式010-85849685
/// <summary>
/// 是不是中国电话,格式010-85849685
/// </summary>
/// <param name="source"></param>
/// <returns></returns>
public static bool IsTel(string source)
{
return Regex.IsMatch(source, @"^\d{3,4}-?\d{6,8}$", RegexOptions.IgnoreCase);
}
#endregion
#region 邮政编码 6个数字
/// <summary>
/// 邮政编码 6个数字
/// </summary>
/// <param name="source"></param>
/// <returns></returns>
public static bool IsPostCode(string source)
{
return Regex.IsMatch(source, @"^\d{6}$", RegexOptions.IgnoreCase);
}
#endregion
#region 中文
/// <summary>
/// 中文
/// </summary>
/// <param name="source"></param>
/// <returns></returns>
public static bool IsChinese(string source)
{
return Regex.IsMatch(source, @"^[\u4e00-\u9fa5]+$", RegexOptions.IgnoreCase);
}
public static bool hasChinese(string source)
{
return Regex.IsMatch(source, @"[\u4e00-\u9fa5]+", RegexOptions.IgnoreCase);
}
#endregion
#region 验证是不是正常字符 字母,数字,下划线的组合
/// <summary>
/// 验证是不是正常字符 字母,数字,下划线的组合
/// </summary>
/// <param name="source"></param>
/// <returns></returns>
public static bool IsNormalChar(string source)
{
return Regex.IsMatch(source, @"[\w\d_]+", RegexOptions.IgnoreCase);
}
#endregion
#region 验证用户名:必须以字母开头,可以包含字母、数字、“_”、“.”,至少5个字符
/// <summary>
/// 验证用户名:必须以字母开头,可以包含字母、数字、“_”、“.”,至少5个字符
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static bool checkUserId(string str)
{
Regex regex = new Regex("[a-zA-Z]{1}([a-zA-Z0-9]|[._]){4,19}");
if (regex.Match(str).Success)
if (regex.Matches(str)[0].Value.Length == str.Length)
return true;
return false;
}
#endregion
#region 验证是否为小数
bool IsValidDecimal(string strIn)
{
return Regex.IsMatch(strIn, @"[0].d{1,2}|[1]");
}
#endregion
#region 验证年月日
bool IsValidDate(string strIn)
{
return Regex.IsMatch(strIn, @"^2d{3}-(?:0?[1-9]|1[0-2])-(?:0?[1-9]|[1-2]d|3[0-1])(?:0?[1-9]|1d|2[0-3]):(?:0?[1-9]|[1-5]d):(?:0?[1-9]|[1-5]d)$");
}
#endregion
#region 验证日期格式
//检察是否正确的日期格式
public static bool IsDate(string str)
{
//考虑到了4年一度的366天,还有特殊的2月的日期
Regex reg = new Regex(@"^((((1[6-9]|[2-9]\d)\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\d|3[01]))|(((1[6-9]|[2-9]\d)\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\d|30))|(((1[6-9]|[2-9]\d)\d{2})-0?2-(0?[1-9]|1\d|2[0-8]))|(((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-)) (20|21|22|23|[0-1]?\d):[0-5]?\d:[0-5]?\d$");
return reg.IsMatch(str);
}
#endregion
#region 验证后缀名
bool IsValidPostfix(string strIn)
{
return Regex.IsMatch(strIn, @".(?i:gif|jpg)$");
}
#endregion
#region 验证字符是否在4至12之间
bool IsValidByte(string strIn)
{
return Regex.IsMatch(strIn, @"^[a-z]{4,12}$");
}
#endregion
#region 判断字符串是否为数字
/// <summary>
/// 判断字符串是否为数字
/// </summary>
/// <param name="str">待验证的字符窜</param>
/// <returns>bool</returns>
public static bool IsNumber(string str)
{
bool result = true;
foreach (char ar in str)
{
if (!char.IsNumber(ar))
{
result = false;
break;
}
}
return result;
}
#endregion
#region 是否为数字型
/// <summary>
/// 是否为数字型
/// </summary>
/// <param name="strNumber"></param>
/// <returns></returns>
public static bool IsDecimal(string strNumber)
{
return new System.Text.RegularExpressions.Regex(@"^([0-9])[0-9]*(\.\w*)?$").IsMatch(strNumber);
}
#endregion
#region 验证是否包含汉语/全部汉语
/// <summary>
/// 验证是否包含汉语
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static bool IsHanyu(string str)
{
Regex regex = new Regex("[\u4e00-\u9fa5]");
if (regex.Match(str).Success)
return true;
else
return false;
}
/// <summary>
/// 验证是否全部汉语
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static bool IsHanyuAll(string str)
{
Regex regex = new Regex("[\u4e00-\u9fa5]");
//匹配的内容长度和被验证的内容长度相同时,验证通过
if (regex.Match(str).Success)
if (regex.Matches(str).Count == str.Length)
return true;
//其它,未通过
return false;
}
#endregion
}
}