using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Serialization;
namespace SLH.SSDMS.Common
{
///
/// 字符串通用类
///
public static class StringExtension
{
///
/// 用于判断是否为空字符
///
///
///
public static bool IsBlank(this string s)
{
return s == null || (s.Trim().Length == 0);
}
///
/// 用于判断是否不为空字符
///
///
///
public static bool IsNotBlank(this string s)
{
return !s.IsBlank();
}
///
/// 判断是否为有效的Email地址
///
///
///
public static bool IsValidEmail(this string s)
{
if (!s.IsBlank())
{
const string pattern = @"^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$";
return Regex.IsMatch(s, pattern);
}
return false;
}
///
/// 验证是否是合法的电话号码
///
///
///
public static bool IsValidPhone(this string s)
{
if (!s.IsBlank())
{
return Regex.IsMatch(s, @"^\+?((\d{2,4}(-)?)|(\(\d{2,4}\)))*(\d{0,16})*$");
}
return true;
}
///
/// 验证是否是合法的手机号码
///
///
///
public static bool IsValidMobile(this string s)
{
if (!s.IsBlank())
{
return Regex.IsMatch(s, @"^\+?\d{0,4}?[1][3-8]\d{9}$");
}
return false;
}
///
/// 验证是否是合法的邮编
///
///
///
public static bool IsValidZipCode(this string s)
{
if (!s.IsBlank())
{
return Regex.IsMatch(s, @"[1-9]\d{5}(?!\d)");
}
return true;
}
///
/// 验证是否是合法的传真
///
///
///
public static bool IsValidFax(this string s)
{
if (!s.IsBlank())
{
return Regex.IsMatch(s, @"(^[0-9]{3,4}\-[0-9]{7,8}$)|(^[0-9]{7,8}$)|(^\([0-9]{3,4}\)[0-9]{3,8}$)|(^0{0,1}13[0-9]{9}$)");
}
return true;
}
///
/// 检查字符串是否为有效的INT32数字
///
///
///
public static bool IsInt32(this string val)
{
if (IsBlank(val))
return false;
Int32 k;
return Int32.TryParse(val, out k);
}
///
/// 检查字符串是否为有效的INT64数字
///
///
///
public static bool IsInt64(this string val)
{
if (IsBlank(val))
return false;
Int64 k;
return Int64.TryParse(val, out k);
}
///
/// 检查字符串是否为有效的Decimal
///
///
///
public static bool IsDecimal(this string val)
{
if (IsBlank(val))
return false;
decimal d;
return Decimal.TryParse(val, out d);
}
///
/// 将字符串转换成MD5加密字符串
///
///
///
public static string ToMD5(this string orgStr)
{
MD5 md5Hasher = MD5.Create();
// Convert the input string to a byte array and compute the hash.
byte[] data = md5Hasher.ComputeHash(Encoding.Default.GetBytes(orgStr));
// Create a new Stringbuilder to collect the bytes
// and create a string.
StringBuilder sBuilder = new StringBuilder();
// Loop through each byte of the hashed data
// and format each one as a hexadecimal string.
for (int i = 0; i < data.Length; i++)
{
sBuilder.Append(data[i].ToString("x2"));
}
// Return the hexadecimal string.
return sBuilder.ToString();
}
///
/// 将对象序列化成XML
///
///
///
///
public static string ToXML(this T obj) where T : class
{
return ToXML(obj, Encoding.Default.BodyName);
}
public static string ToXML(this T obj, string encodeName) where T : class
{
if (obj == null) throw new ArgumentNullException("obj", "obj is null.");
if (obj is string) throw new ApplicationException("obj can't be string object.");
Encoding en = Encoding.GetEncoding(encodeName);
XmlSerializer serial = new XmlSerializer(typeof(T));
XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add("", "");
MemoryStream ms = new MemoryStream();
XmlTextWriter xt = new XmlTextWriter(ms, en);
serial.Serialize(xt, obj, ns);
xt.Close();
ms.Close();
return en.GetString(ms.ToArray());
}
///
/// 将XML字符串反序列化成对象实体
///
///
///
///
public static T Deserial(this string s) where T : class
{
return Deserial(s, Encoding.Default.BodyName);
}
public static T Deserial(this string s, string encodeName) where T : class
{
if (s.IsBlank())
{
throw new ApplicationException("xml string is null or empty.");
}
XmlSerializer serial = new XmlSerializer(typeof(T));
XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add("", "");
return (T)serial.Deserialize(new StringReader(s));
}
///
/// 获取扩展名
///
///
///
public static string GetExt(this string s)
{
string ret = string.Empty;
if (s.Contains('.'))
{
string[] temp = s.Split('.');
ret = temp[temp.Length - 1];
}
return ret;
}
///
/// 验证QQ格式
///
///
///
public static bool IsValidQQ(this string s)
{
if (!s.IsBlank())
{
return Regex.IsMatch(s, @"^[1-9]\d{4,15}$");
}
return false;
}
///
/// 字符串转成Int
///
///
///
public static int ToInt(this string s)
{
int res;
int.TryParse(s, out res);
return res;
}
///
/// 生成单号
///
///
///
public static string GenerateBillsNo(this string s)
{
return string.Concat(s, DateTime.Now.ToString("yyyyMMddHHmmss"));
}
///
/// 生成初次入库单单号,出库单单号,盘点单单号
///
///
///
public static string GenerateInBillsNo(this string s)
{
return string.Concat(s, DateTime.Now.ToString("yyMMdd") + "001");
}
///
/// 生成初次出库单单号
///
///
///
public static string GenerateOutBillsNo(this string s)
{
return string.Concat(s, DateTime.Now.ToString("yyyyMMdd") + "0001");
}
///
/// 生成打印单号
///
///
///
public static string GenerateNumber(this string s)
{
Random rd = new Random(GetRandomSeed());
string rdNext = rd.Next(000, 999).ToString();
var str = string.Format("{0:000}", int.Parse(s));
return DateTime.Now.Date.ToString("MMdd") + rdNext + str;
}
///
/// 生成打印单号,出库单流水号
///
///
///
public static string GeneratePrintNo(this string s)
{
var str = string.Format("{0:000}", int.Parse(s));
return str;
}
///
/// 生成出库单流水号
///
///
///
public static string GenerateOutBillsFlowNo(this string s)
{
var str = string.Format("{0:0000}", int.Parse(s));
return str;
}
static int GetRandomSeed()
{
byte[] bytes = new byte[4];
RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
rng.GetBytes(bytes);
return BitConverter.ToInt32(bytes, 0);
}
}
}