企业如何利用HarmonyOS开发工具提升小程序开发效率与合规性
596
2022-09-16
C#tools(乘风破浪的姐姐第三季)
public class Utils
{
#region 对象转换处理
///
/// 判断对象是否为Int32类型的数字
///
///
///
public static bool IsNumeric(object expression)
{
if (expression != null)
return IsNumeric(expression.ToString());
return false;
}
///
/// 判断对象是否为Int32类型的数字
///
///
///
public static bool IsNumeric(string expression)
{
if (expression != null)
{
string str = expression;
if (str.Length > 0 && str.Length <= 11 && Regex.IsMatch(str, @"^[-]?[0-9]*[.]?[0-9]*$"))
{
if ((str.Length < 10) || (str.Length == 10 && str[0] == '1') || (str.Length == 11 && str[0] == '-' && str[1] == '1'))
return true;
}
}
return false;
}
///
/// 截取字符串长度,超出部分使用后缀suffix代替,比如abcdevfddd取前3位,后面使用...代替
///
///
///
///
///
public static string SubStrAddSuffix(string orginStr, int length, string suffix)
{
string ret = orginStr;
if (orginStr.Length > length)
{
ret = orginStr.Substring(0, length) + suffix;
}
return ret;
}
///
/// 是否为Double类型
///
///
///
public static bool IsDouble(object expression)
{
if (expression != null)
return Regex.IsMatch(expression.ToString(), @"^([0-9])[0-9]*(\.\w*)?$");
return false;
}
///
/// 检测是否符合email格式
///
/// 要判断的email字符串
///
public static bool IsValidEmail(string strEmail)
{
return Regex.IsMatch(strEmail, @"^[\w\.]+([-]\w+)*@[A-Za-z0-9-_]+[\.][A-Za-z0-9-_]");
}
public static bool IsValidDoEmail(string strEmail)
{
return Regex.IsMatch(strEmail, @"^@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$");
}
///
/// 检测是否是正确的Url
///
/// 要验证的Url
///
public static bool IsURL(string strUrl)
{
return Regex.IsMatch(strUrl, @"^(http|https)\://([a-zA-Z0-9\.\-]+(\:[a-zA-Z0-9\.&%\$\-]+)*@)*((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])|localhost|([a-zA-Z0-9\-]+\.)*[a-zA-Z0-9\-]+\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{1,10}))(\:[0-9]+)*(/($|[a-zA-Z0-9\.\,\?\'\\\+&%\$#\=~_\-]+))*$");
}
///
/// 将字符串转换为数组
///
/// 字符串
///
public static string[] GetStrArray(string str)
{
return str.Split(new char[',']);
}
///
/// 将数组转换为字符串
///
/// List
/// 分隔符
///
public static string GetArrayStr(List
{
StringBuilder sb = new StringBuilder();
for (int i = 0; i < list.Count; i++)
{
if (i == list.Count - 1)
{
sb.Append(list[i]);
}
else
{
sb.Append(list[i]);
sb.Append(speater);
}
}
return sb.ToString();
}
///
/// object型转换为bool型
///
/// 要转换的字符串
/// 缺省值
///
public static bool StrToBool(object expression, bool defValue)
{
if (expression != null)
return StrToBool(expression, defValue);
return defValue;
}
///
/// string型转换为bool型
///
/// 要转换的字符串
/// 缺省值
///
public static bool StrToBool(string expression, bool defValue)
{
if (expression != null)
{
if (string.Compare(expression, "true", true) == 0)
return true;
else if (string.Compare(expression, "false", true) == 0)
return false;
}
return defValue;
}
///
/// 将对象转换为Int32类型
///
/// 要转换的字符串
/// 缺省值
///
public static int ObjToInt(object expression, int defValue)
{
if (expression != null)
return StrToInt(expression.ToString(), defValue);
return defValue;
}
///
/// 将字符串转换为Int32类型
///
/// 要转换的字符串
/// 缺省值
///
public static int StrToInt(string expression, int defValue)
{
if (string.IsNullOrEmpty(expression) || expression.Trim().Length >= 11 || !Regex.IsMatch(expression.Trim(), @"^([-]|[0-9])[0-9]*(\.\w*)?$"))
return defValue;
int rv;
if (Int32.TryParse(expression, out rv))
return rv;
return Convert.ToInt32(StrToFloat(expression, defValue));
}
///
/// Object型转换为decimal型
///
/// 要转换的字符串
/// 缺省值
///
public static decimal ObjToDecimal(object expression, decimal defValue)
{
if (expression != null)
return StrToDecimal(expression.ToString(), defValue);
return defValue;
}
///
/// string型转换为decimal型
///
/// 要转换的字符串
/// 缺省值
///
public static decimal StrToDecimal(string expression, decimal defValue)
{
if ((expression == null) || (expression.Length > 10))
return defValue;
decimal intValue = defValue;
if (expression != null)
{
bool IsDecimal = Regex.IsMatch(expression, @"^([-]|[0-9])[0-9]*(\.\w*)?$");
if (IsDecimal)
decimal.TryParse(expression, out intValue);
}
return intValue;
}
///
/// Object型转换为float型
///
/// 要转换的字符串
/// 缺省值
///
public static float ObjToFloat(object expression, float defValue)
{
if (expression != null)
return StrToFloat(expression.ToString(), defValue);
return defValue;
}
///
/// string型转换为float型
///
/// 要转换的字符串
/// 缺省值
///
public static float StrToFloat(string expression, float defValue)
{
if ((expression == null) || (expression.Length > 10))
return defValue;
float intValue = defValue;
if (expression != null)
{
bool IsFloat = Regex.IsMatch(expression, @"^([-]|[0-9])[0-9]*(\.\w*)?$");
if (IsFloat)
float.TryParse(expression, out intValue);
}
return intValue;
}
///
/// 将对象转换为日期时间类型
///
/// 要转换的字符串
/// 缺省值
///
public static DateTime StrToDateTime(string str, DateTime defValue)
{
if (!string.IsNullOrEmpty(str))
{
DateTime dateTime;
if (DateTime.TryParse(str, out dateTime))
return dateTime;
}
return defValue;
}
///
/// 将对象转换为日期时间类型
///
/// 要转换的字符串
///
public static DateTime StrToDateTime(string str)
{
return StrToDateTime(str, DateTime.Now);
}
///
/// 将对象转换为日期时间类型
///
/// 要转换的对象
///
public static DateTime ObjectToDateTime(object obj)
{
return StrToDateTime(obj.ToString());
}
///
/// 将对象转换为日期时间类型
///
/// 要转换的对象
/// 缺省值
///
public static DateTime ObjectToDateTime(object obj, DateTime defValue)
{
return StrToDateTime(obj.ToString(), defValue);
}
///
/// 将对象转换为字符串
///
/// 要转换的对象
///
public static string ObjectToStr(object obj)
{
if (obj == null)
return "";
return obj.ToString().Trim();
}
///
/// 将对象转换为Int类型
///
///
///
public static int ObjToInt(object obj)
{
if (isNumber(obj))
{
return int.Parse(obj.ToString());
}
else
{
return 0;
}
}
///
/// 判断对象是否可以转成int型
///
///
///
public static bool isNumber(object o)
{
int tmpInt;
if (o == null)
{
return false;
}
if (o.ToString().Trim().Length == 0)
{
return false;
}
if (!int.TryParse(o.ToString(), out tmpInt))
{
return false;
}
else
{
return true;
}
}
#endregion
#region 分割字符串
///
/// 分割字符串
///
public static string[] SplitString(string strContent, string strSplit)
{
if (!string.IsNullOrEmpty(strContent))
{
if (strContent.IndexOf(strSplit) < 0)
return new string[] { strContent };
return Regex.Split(strContent, Regex.Escape(strSplit), RegexOptions.IgnoreCase);
}
else
return new string[0] { };
}
///
/// 分割字符串
///
///
public static string[] SplitString(string strContent, string strSplit, int count)
{
string[] result = new string[count];
string[] splited = SplitString(strContent, strSplit);
for (int i = 0; i < count; i++)
{
if (i < splited.Length)
result[i] = splited[i];
else
result[i] = string.Empty;
}
return result;
}
#endregion
#region 删除最后结尾的一个逗号
///
/// 删除最后结尾的一个逗号
///
public static string DelLastComma(string str)
{
if (str.Length < 1)
{
return "";
}
return str.Substring(0, str.LastIndexOf(","));
}
#endregion
#region 删除最后结尾的指定字符后的字符
///
/// 删除最后结尾的指定字符后的字符
///
public static string DelLastChar(string str, string strchar)
{
if (string.IsNullOrEmpty(str))
return "";
if (str.LastIndexOf(strchar) >= 0 && str.LastIndexOf(strchar) == str.Length - 1)
{
return str.Substring(0, str.LastIndexOf(strchar));
}
return str;
}
#endregion
#region 生成指定长度的字符串
///
/// 生成指定长度的字符串,即生成strLong个str字符串
///
/// 生成的长度
/// 以str生成字符串
///
public static string StringOfChar(int strLong, string str)
{
string ReturnStr = "";
for (int i = 0; i < strLong; i++)
{
ReturnStr += str;
}
return ReturnStr;
}
#endregion
#region 生成日期随机码
///
/// 生成日期随机码
///
///
public static string GetRamCode()
{
#region
return DateTime.Now.ToString("yyyyMMddHHmmssffff");
#endregion
}
#endregion
#region 生成随机字母或数字
///
/// 生成随机数字
///
/// 生成长度
///
public static string Number(int Length)
{
return Number(Length, false);
}
///
/// 生成随机数字
///
/// 生成长度
/// 是否要在生成前将当前线程阻止以避免重复
///
public static string Number(int Length, bool Sleep)
{
if (Sleep)
System.Threading.Thread.Sleep(3);
string result = "";
System.Random random = new System.Random();
for (int i = 0; i < Length; i++)
{
result += random.Next(10).ToString();
}
return result;
}
///
/// 根据日期和随机码生成订单号
///
///
public static string GetOrderNumber()
{
string num = DateTime.Now.ToString("yyMMddHHmmss");//yyyyMMddHHmmssms
return num + Number(2, true).ToString();
}
private static int Next(int numSeeds, int length)
{
byte[] buffer = new byte[length];
System.Security.Cryptography.RNGCryptoServiceProvider Gen = new System.Security.Cryptography.RNGCryptoServiceProvider();
Gen.GetBytes(buffer);
uint randomResult = 0x0;//这里用uint作为生成的随机数
for (int i = 0; i < length; i++)
{
randomResult |= ((uint)buffer[i] << ((length - 1 - i) * 8));
}
return (int)(randomResult % numSeeds);
}
#endregion
#region TXT代码转换成HTML格式
///
/// 字符串字符处理
///
/// 等待处理的字符串
///
/// //把TXT代码转换成HTML格式
public static String ToHtml(string Input)
{
StringBuilder sb = new StringBuilder(Input);
sb.Replace("'", "'");
sb.Replace("&", "&");
sb.Replace("<", "<");
sb.Replace(">", ">");
sb.Replace("\r\n", "
");
sb.Replace("\n", "
");
sb.Replace("\t", " ");
//sb.Replace(" ", " ");
return sb.ToString();
}
#endregion
#region HTML代码转换成TXT格式
///
/// 字符串字符处理
///
/// 等待处理的字符串
///
/// //把HTML代码转换成TXT格式
public static
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~