asp.net 字符帮助类 类型转换类
/// <summary>
/// 字符帮助类
/// </summary>
public class StringHelper
{
private static readonly Regex RegEmail = new Regex("^\\s*([A-Za-z0-9_-]+(\\.\\w+)*@([\\w-]+\\.)+\\w{2,3})\\s*$", RegexOptions.IgnoreCase); //验证邮箱正则
private static readonly Regex RegHasCHZN = new Regex("[\u4e00-\u9fa5]", RegexOptions.IgnoreCase);//验证是否含中文正则
private static readonly Regex RegCHZN = new Regex(@"^[\u4e00-\u9fa5]{1,}$", RegexOptions.IgnoreCase);//验证是否为中文正则
private static readonly Regex RegPhone = new Regex("(^(\\d{11})$|^((\\d{7,8})|(\\d{4}|\\d{3})-(\\d{7,8})|(\\d{4}|\\d{3})-(\\d{7,8})-(\\d{4}|\\d{3}|\\d{2}|\\d{1})|(\\d{7,8})-(\\d{4}|\\d{3}|\\d{2}|\\d{1}))$)", RegexOptions.IgnoreCase); //验证电话和手机号码正则
private static readonly Regex RegUrl = new Regex(@"http(s)?://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?",RegexOptions.IgnoreCase);//验证Url正则
private static readonly Regex RegNumber = new Regex(@"^[0-9]*$", RegexOptions.IgnoreCase);//验证纯数字l正则 #region 判断IP格式 /// <summary>
/// 判断是否是IP地址格式 0.0.0.0
/// </summary>
/// <param name="input">待判断的IP地址</param>
/// <returns>true or false</returns>
public static bool IsIPAddress(string input)
{
if (input.IsNullOrEmpty() || input.Length < || input.Length > ) return false; const string regFormat = @"^d{1,3}[.]d{1,3}[.]d{1,3}[.]d{1,3}$"; var regex = new Regex(regFormat, RegexOptions.IgnoreCase);
return regex.IsMatch(input);
} #endregion #region 判断邮箱格式 /// <summary>
/// 判断邮箱格式
/// </summary>
/// <param name="input">文本信息</param>
/// <returns>验证结果</returns>
public static bool IsEmail(string input)
{
if (input.IsNullOrEmpty())
return false;
Match m = RegEmail.Match(input);
return m.Success;
}
#endregion #region 中文字符检测 /// <summary>
/// 检测是否有中文字符
/// </summary>
/// <param name="input">文本信息</param>
/// <returns>检测结果</returns>
public static bool IsContainCHZN(string input)
{
if (input.IsNullOrEmpty())
return false;
Match m = RegHasCHZN.Match(input);
return m.Success;
}
#endregion #region 判断是否中文
/// <summary>
/// 判断参数是否为中文字符
/// </summary>
/// <param name="input">文本信息</param>
/// <returns></returns>
public static bool IsChinese(string input)
{
if (input.IsNullOrEmpty())
return false;
Match m = RegCHZN.Match(input);
return m.Success;
}
#endregion #region 判断电话号码
// 电话号码和手机号码检查
/// <summary>
/// 电话号码和手机号码检查
/// </summary>
/// <param name="input">电话号码或手机号码</param>
/// <returns>匹配结果</returns>
public static bool IsPhone(string input)
{
if (input.IsNullOrEmpty())
return false;
Match m = RegPhone.Match(input);
return m.Success;
}
#endregion #region 判断Url合法性
/// <summary>
/// 验证请求的url是否合法
/// </summary>
/// <param name="input">输入参数</param>
/// <returns></returns>
public static bool IsUrl(string input)
{
if (input.IsNullOrEmpty())
return false;
Match m = RegUrl.Match(input);
return m.Success;
}
#endregion #region 判断是否数字
/// <summary>
/// 验证参数是否为数字
/// </summary>
/// <param name="input">输入字符串</param>
/// <returns></returns>
public static bool IsNumberic(string input)
{
if (input.IsNullOrEmpty())
return false;
Match m = RegNumber.Match(input);
return m.Success;
}
#endregion #region 字符串截取
/// <summary>
/// 检查字符串最大长度,返回指定长度的串
/// </summary>
/// <param name="input">输入字符串</param>
/// <param name="maxLength">最大长度</param>
/// <returns></returns>
public static string TextSbustring(string input, int maxLength)
{
if (input.IsNullOrEmpty())
return null;
input = input.Trim();
if (input.Length > maxLength)//按最大长度截取字符串
input = input.Substring(, maxLength);
return input;
}
#endregion #region 判断是否含关键词
/// <summary>
/// 检查是否包含SQL关键字
/// </summary>
/// <param name="input">被检查的字符串</param>
/// <returns>存在SQL关键字返回true,不存在返回false</returns>
public static bool IsContainSQLKeyWord(string input)
{
if (input.IsNullOrEmpty())
return false;
string strKeyWord = @"select|insert|delete|from|count(|drop table|update|truncate|asc(|mid(|char(|xp_cmdshell|exec master|netlocalgroup administrators|:|net user|""|or|and";
string strRegex = @"[-|;|,|/|(|)|[|]|}|{|%|@|*|!|']";
if (Regex.IsMatch(input, strKeyWord, RegexOptions.IgnoreCase) || Regex.IsMatch(input, strRegex))
return true;
return false;
} /// <summary>
/// 检查是否含有关键字
/// </summary>
/// <param name="input">被检查的字符串</param>
/// <returns></returns>
public static bool IsContainKeywords(string input)
{
if (input.IsNullOrEmpty())
return false;
string sLowerStr = input.ToLower();
string sRxStr = @"(\sand\s)|(\sand\s)|(\slike\s)|(select\s)|(insert\s)|(delete\s)|(update\s[\s\S].*\sset)|(create\s)|(\stable)|(<[iframe|/iframe|script|/script])|(')|(\sexec)|(declare)|(\struncate)|(\smaster)|(\sbackup)|(\smid)|(\scount)|(cast)|(%)|(\sadd\s)|(\salter\s)|(\sdrop\s)|(\sfrom\s)|(\struncate\s)|(\sxp_cmdshell\s)";
Regex sRx = new Regex(sRxStr);
return sRx.IsMatch(sLowerStr, );
}
#endregion #region 判断是否包含空格
/// <summary>
/// 检查是否包含空格
/// </summary>
/// <param name="input">被检查的字符串</param>
/// <returns>存在空格返回true,不存在返回false</returns>
public static bool IsContainSpace(string input)
{
if (input.IsNullOrEmpty())
return false;
if (input.IndexOf(" ") >= )
return true;
else
return false;
}
#endregion
}
/// <summary>
/// 将对象转化为Int32类型
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public static int GetInt(this object input)
{
return Convert.ToInt32(input);
}
/// <summary>
/// 将对象转化为Int64类型
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public static long GetLong(this object input)
{
return Convert.ToInt64(input);
}
/// <summary>
/// 将对象转化为DateTime类型
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public static DateTime GetDateTime(this object input)
{
return Convert.ToDateTime(input);
}
/// <summary>
/// 将对象转化为DateTime类型
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public static DateTime? GetDateTimeNullable(this object input)
{
if (input == null||input.ToString().IsNullOrEmpty())
return null;
return Convert.ToDateTime(input);
}
/// <summary>
/// 将对象转化为Decimal类型
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public static Decimal GetDecimal(this object input)
{
return Convert.ToDecimal(input);
}
/// <summary>
/// 将对象转化为Boolean类型
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public static bool GetBool(this object input)
{
return Convert.ToBoolean(input);
}
/// <summary>
/// 字符串是否为空
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static bool IsNullOrEmpty(this string str)
{
return str == null || str == string.Empty || str == "";
}
/// <summary>
/// 字符串是否不为空
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static bool IsNotNullOrEmpty(this string str)
{
return str != null && str != string.Empty && str != "";
}
/// <summary>
/// 判断是否是大于或等于0的整数
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public static bool IsNumeric(this string input)
{
int output = ;
return int.TryParse(input, out output);
}
/// <summary>
/// 判断是否是合法日期
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public static bool IsDateTime(this string input)
{
DateTime tmp;
return DateTime.TryParse(input, out tmp);
}
/// <summary>
/// 判断字符串是否是Decimal
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public static bool IsDecimal(this string input)
{
Decimal output = ;
return decimal.TryParse(input, out output);
}
/// <summary>
/// 判断对象是否是Boolean
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public static bool IsBoolean(this string input)
{
bool output = false;
return Boolean.TryParse(input, out output);
}
asp.net 字符帮助类 类型转换类的更多相关文章
- C#类型转换类(通用类)
// /// 类型转换类 /// 处理数据库获取字段为空的情况 /// public static class DBConvert { #reg ...
- C++ 隐式类类型转换
<C++ Primer>中提到: “可以用 单个形参来调用 的构造函数定义了从 形参类型 到 该类类型 的一个隐式转换.” 这里应该注意的是, “可以用单个形参进行调用” 并不是指构造函数 ...
- 字节流和字符流(InputStream类和OutputStream类)
java流包括字节流和字符流,字节流通过I/O设备以字节数据的方式读入,而字符流则是通过字节流读入数据转换成字符"流"的形式由用户驱使. InputStream是所有字节输入流的父 ...
- C++ 隐式类类型转换和转换操作符
隐式类类型转换 C++语言定义了内置类型之间的几个自动转换.也可以定义如何将其他类型的对象隐式转换为我们的类类型,或将我们的类类型的对象隐式转换为其他类型.为了定义到类类型的隐式转换,需要定义合适的构 ...
- C#--数组、字符与字符串--StringBuilder类、字符与字符串、字符及转义字符
C#--数组 字符与字符串--StringBuilder类 字符与字符串 字符及转义字符
- [C++]复制构造函数、赋值操作符与隐式类类型转换
问题:现有类A定义如下: class A{public: A(int a) //构造函数 { ...
- XML序列化 判断是否是手机 字符操作普通帮助类 验证数据帮助类 IO帮助类 c# Lambda操作类封装 C# -- 使用反射(Reflect)获取dll文件中的类型并调用方法 C# -- 文件的压缩与解压(GZipStream)
XML序列化 #region 序列化 /// <summary> /// XML序列化 /// </summary> /// <param name="ob ...
- (1)ASP.NET Core 应用启动Startup类简介
1.前言 Core与早期版本的 ASP.NET 对比,配置应用程序的方式的 Global.asax.FilterConfig.cs和RouteConfig.cs 都被Program.cs 和 Star ...
- 字符输出流_Writer类&FileWrite类介绍和字符输出流的基本使用_写出单个字符到文件
字符输出流_Writer类&FileWrite类介绍 java.io.Writer:字符输出流,是所有字符输出流的最顶层的父类,是一个抽象类 共性抽象方法: void write(int c) ...
随机推荐
- 如何修正Feedly文章中文標題亂碼或無法正常顯示的問題
在7月1日Google關閉Reader之前,我想應該有許多人都已經從Google Reader移到其他服務上了,其中受益最大的者莫過於Feedly了,一下子就吸收了幾百萬的用戶,而我也是其中之一,由於 ...
- POJ2248 A Knight's Journey(DFS)
题目链接. 题目大意: 给定一个矩阵,马的初始位置在(0,0),要求给出一个方案,使马走遍所有的点. 列为数字,行为字母,搜索按字典序. 分析: 用 vis[x][y] 标记是否已经访问.因为要搜索所 ...
- BZOJ 1021 [SHOI2008]Debt 循环的债务
1021: [SHOI2008]Debt 循环的债务 Time Limit: 1 Sec Memory Limit: 162 MBSubmit: 694 Solved: 356[Submit][S ...
- 将银行读卡设备读取到的身份证头像Bitmap属性转换成路径
需求是这样的,在项目开发的时候要求读取身份证,读到身份证的所有信息(信息里面包括头像属性,类型是Bitmap的).然后服务器要求我传过去的头像信息是String类型的Uri路径. 这是读卡器读到的身份 ...
- HDU_1401——同步双向BFS,八进制位运算压缩,map存放hash
这个速度比分步快一点,内存占的稍微多一点 Problem Description Solitaire is a game played on a chessboard 8x8. The rows an ...
- [LeetCode] 310. Minimum Height Trees 解题思路
For a undirected graph with tree characteristics, we can choose any node as the root. The result gra ...
- Event — Windows API
Event即事件是一种用于进行线程/进程间同步的对象,事件有置位和复位两种状态,当线程通过waiting functions等待Event对象置位时该线程将进入阻塞状态,当该Event对象被置位或等待 ...
- DB2 递归
公司一直用递归来生成组织机构的树状图.看了上面的文档,应该立马就能写了. 不过前几天,有个功能涉及到下面的状况: 需要组织机构等级为1级的下面所有子机构.且按照一级组织机构分组.大家都觉得很难,哥就一 ...
- [LeetCode] Word Search [37]
题目 Given a 2D board and a word, find if the word exists in the grid. The word can be constructed fro ...
- C primer plus 读书笔记第一章
写在前面: 算法和数据结构是计算机学习的基础,而大部分书籍是用C/C++编写.所以有了把C语言重新学一遍的想法.这个系列主要是记录看C primer plus的一些笔记和部分课后习题的答案,不会总结的 ...