|
|
|
|
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
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 字符串通用类
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static class StringExtension
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 用于判断是否为空字符
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="s"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static bool IsBlank(this string s)
|
|
|
|
|
{
|
|
|
|
|
return s == null || (s.Trim().Length == 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 用于判断是否不为空字符
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="s"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static bool IsNotBlank(this string s)
|
|
|
|
|
{
|
|
|
|
|
return !s.IsBlank();
|
|
|
|
|
}
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 判断是否为有效的Email地址
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="s"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static bool IsValidEmail(this string s)
|
|
|
|
|
{
|
|
|
|
|
if (!s.IsBlank())
|
|
|
|
|
{
|
|
|
|
|
const string pattern = @"^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$";
|
|
|
|
|
return Regex.IsMatch(s, pattern);
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 验证是否是合法的电话号码
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="s"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 验证是否是合法的手机号码
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="s"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static bool IsValidMobile(this string s)
|
|
|
|
|
{
|
|
|
|
|
if (!s.IsBlank())
|
|
|
|
|
{
|
|
|
|
|
return Regex.IsMatch(s, @"^\+?\d{0,4}?[1][3-8]\d{9}$");
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 验证是否是合法的邮编
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="s"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static bool IsValidZipCode(this string s)
|
|
|
|
|
{
|
|
|
|
|
if (!s.IsBlank())
|
|
|
|
|
{
|
|
|
|
|
return Regex.IsMatch(s, @"[1-9]\d{5}(?!\d)");
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 验证是否是合法的传真
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="s"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 检查字符串是否为有效的INT32数字
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="val"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static bool IsInt32(this string val)
|
|
|
|
|
{
|
|
|
|
|
if (IsBlank(val))
|
|
|
|
|
return false;
|
|
|
|
|
Int32 k;
|
|
|
|
|
return Int32.TryParse(val, out k);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 检查字符串是否为有效的INT64数字
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="val"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static bool IsInt64(this string val)
|
|
|
|
|
{
|
|
|
|
|
if (IsBlank(val))
|
|
|
|
|
return false;
|
|
|
|
|
Int64 k;
|
|
|
|
|
return Int64.TryParse(val, out k);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 检查字符串是否为有效的Decimal
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="val"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static bool IsDecimal(this string val)
|
|
|
|
|
{
|
|
|
|
|
if (IsBlank(val))
|
|
|
|
|
return false;
|
|
|
|
|
decimal d;
|
|
|
|
|
return Decimal.TryParse(val, out d);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 将字符串转换成MD5加密字符串
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="orgStr"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 将对象序列化成XML
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="T"></typeparam>
|
|
|
|
|
/// <param name="obj"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static string ToXML<T>(this T obj) where T : class
|
|
|
|
|
{
|
|
|
|
|
return ToXML(obj, Encoding.Default.BodyName);
|
|
|
|
|
}
|
|
|
|
|
public static string ToXML<T>(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());
|
|
|
|
|
}
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 将XML字符串反序列化成对象实体
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="T"></typeparam>
|
|
|
|
|
/// <param name="s"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static T Deserial<T>(this string s) where T : class
|
|
|
|
|
{
|
|
|
|
|
return Deserial<T>(s, Encoding.Default.BodyName);
|
|
|
|
|
}
|
|
|
|
|
public static T Deserial<T>(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));
|
|
|
|
|
}
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 获取扩展名
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="s"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 验证QQ格式
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="s"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static bool IsValidQQ(this string s)
|
|
|
|
|
{
|
|
|
|
|
if (!s.IsBlank())
|
|
|
|
|
{
|
|
|
|
|
return Regex.IsMatch(s, @"^[1-9]\d{4,15}$");
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 字符串转成Int
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="s"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static int ToInt(this string s)
|
|
|
|
|
{
|
|
|
|
|
int res;
|
|
|
|
|
int.TryParse(s, out res);
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 生成单号
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="s"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static string GenerateBillsNo(this string s)
|
|
|
|
|
{
|
|
|
|
|
return string.Concat(s, DateTime.Now.ToString("yyyyMMddHHmmss"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 生成初次入库单单号,出库单单号,盘点单单号
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="s"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static string GenerateInBillsNo(this string s)
|
|
|
|
|
{
|
|
|
|
|
return string.Concat(s, DateTime.Now.ToString("yyMMdd") + "001");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 生成初次出库单单号
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="s"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static string GenerateOutBillsNo(this string s)
|
|
|
|
|
{
|
|
|
|
|
return string.Concat(s, DateTime.Now.ToString("yyyyMMdd") + "0001");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 生成打印单号
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="s"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 生成打印单号,出库单流水号
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="s"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static string GeneratePrintNo(this string s)
|
|
|
|
|
{
|
|
|
|
|
var str = string.Format("{0:000}", int.Parse(s));
|
|
|
|
|
return str;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 生成出库单流水号
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="s"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|