初始化
This commit is contained in:
132
Face.Services/Extensions/SafeInputExtensions.cs
Normal file
132
Face.Services/Extensions/SafeInputExtensions.cs
Normal file
@@ -0,0 +1,132 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace System
|
||||
{
|
||||
/// <summary>
|
||||
/// 字符串转换扩展
|
||||
/// </summary>
|
||||
public static class SafeInputExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// 将字符串转换成Int类型,如果出错则返回0
|
||||
/// </summary>
|
||||
/// <param name="str"></param>
|
||||
/// <returns></returns>
|
||||
public static short ToShort(this object str)
|
||||
{
|
||||
short result = 0;
|
||||
if (str != null)
|
||||
{
|
||||
short.TryParse(str.ToString(), out result);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 将字符串转换成Int类型,如果出错则返回0
|
||||
/// </summary>
|
||||
/// <param name="str"></param>
|
||||
/// <returns></returns>
|
||||
public static int ToInt(this object str)
|
||||
{
|
||||
int result = 0;
|
||||
if (str != null)
|
||||
{
|
||||
int.TryParse(str.ToString(), out result);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 将字符串转换成decimal类型,如果出错则返回0
|
||||
/// </summary>
|
||||
/// <param name="str"></param>
|
||||
/// <returns></returns>
|
||||
public static double ToDouble(this object str)
|
||||
{
|
||||
double result = 0.0;
|
||||
if (str != null)
|
||||
{
|
||||
double.TryParse(str.ToString(), out result);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public static decimal ToDecimal(this object str)
|
||||
{
|
||||
decimal result = 0m;
|
||||
if (str != null)
|
||||
{
|
||||
decimal.TryParse(str.ToString(), out result);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public static string ToNone(this object str, string extStr)
|
||||
{
|
||||
if (str != null && !string.IsNullOrEmpty(str.ToString()))
|
||||
{
|
||||
return str + " " + extStr;
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 将字符串转换成真假
|
||||
/// </summary>
|
||||
/// <param name="str"></param>
|
||||
/// <returns></returns>
|
||||
public static bool ToBool(this object str)
|
||||
{
|
||||
return str != null && str.ToString().Equals("true", StringComparison.CurrentCultureIgnoreCase);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 将字符串转换成GUID,出错则为Guid.NewGuid()
|
||||
/// </summary>
|
||||
/// <param name="str"></param>
|
||||
/// <returns></returns>
|
||||
public static Guid ToGuid(this object str)
|
||||
{
|
||||
Guid result = Guid.NewGuid();
|
||||
if (str != null)
|
||||
{
|
||||
Guid.TryParse(str.ToString(), out result);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public static DateTime ToDateTime(this object str)
|
||||
{
|
||||
DateTime now = DateTime.Now;
|
||||
if (str != null)
|
||||
{
|
||||
DateTime.TryParse(str.ToString(), out now);
|
||||
}
|
||||
return now;
|
||||
}
|
||||
|
||||
public static string ToDateTimeRandom()
|
||||
{
|
||||
return DateTime.Now.ToString("yyyyMMddHHmmss") + new Random().Next(9999).ToString();
|
||||
}
|
||||
|
||||
public static string ToDateString()
|
||||
{
|
||||
return DateTime.Now.ToString("yyyyMMdd");
|
||||
}
|
||||
|
||||
public static string ToBr(this string str)
|
||||
{
|
||||
if (!string.IsNullOrEmpty(str))
|
||||
{
|
||||
return str.Replace("\r\n", "<br>");
|
||||
}
|
||||
return str;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user