using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
namespace FCS.Common
{
///
/// 字符串操作 - 工具方法
///
public static partial class StringHelper
{
#region ContainsChinese(是否包含中文)
///
/// 是否包含中文
///
/// 文本
public static bool ContainsChinese(string text)
{
const string pattern = "[\u4e00-\u9fa5]+";
return Regex.IsMatch(text, pattern);
}
#endregion ContainsChinese(是否包含中文)
#region ContainsNumber(是否包含数字)
///
/// 是否包含数字
///
/// 文本
public static bool ContainsNumber(string text)
{
const string pattern = "[0-9]+";
return Regex.IsMatch(text, pattern);
}
#endregion ContainsNumber(是否包含数字)
#region Distinct(去除重复)
///
/// 去除重复
///
/// 值,范例1:"5555",返回"5",范例2:"4545",返回"45"
public static string Distinct(string value)
{
var array = value.ToCharArray();
return new string(array.Distinct().ToArray());
}
#endregion Distinct(去除重复)
#region 删除最后一个字符之后的字符
///
/// 删除最后结尾的一个逗号
///
public static string DelLastComma(string str)
{
return str.Substring(0, str.LastIndexOf(",", StringComparison.Ordinal));
}
///
/// 删除最后结尾的指定字符后的字符
///
public static string DelLastChar(string str, string strchar)
{
return str.Substring(0, str.LastIndexOf(strchar, StringComparison.Ordinal));
}
///
/// 删除最后结尾的长度
///
///
///
///
public static string DelLastLength(string str, int Length)
{
if (string.IsNullOrEmpty(str))
return "";
str = str.Substring(0, str.Length - Length);
return str;
}
#endregion 删除最后一个字符之后的字符
#region 快速验证一个字符串是否符合指定的正则表达式
///
/// 快速验证一个字符串是否符合指定的正则表达式。
///
/// 正则表达式的内容。
/// 需验证的字符串。
/// 是否合法的bool值。
public static bool QuickValidate(string express, string value)
{
if (value == null) return false;
Regex myRegex = new Regex(express);
if (value.Length == 0)
{
return false;
}
return myRegex.IsMatch(value);
}
#endregion 快速验证一个字符串是否符合指定的正则表达式
///
/// 检测空值,为null则抛出ArgumentNullException异常
///
/// 对象
/// 参数名
public static void CheckNull(this object obj, string parameterName)
{
if (obj == null)
throw new ArgumentNullException(parameterName);
}
///
/// 是否为空
///
/// 值
public static bool IsEmpty(this string value)
{
return string.IsNullOrWhiteSpace(value);
}
///
/// 是否为空
///
/// 值
public static bool IsEmpty(this Guid? value)
{
if (value == null)
return true;
return IsEmpty(value.Value);
}
///
/// 是否为空
///
/// 值
public static bool IsEmpty(this Guid value)
{
if (value == Guid.Empty)
return true;
return false;
}
///
/// 是否为空
///
/// 值
public static bool IsEmpty(this object value)
{
if (value != null && !string.IsNullOrEmpty(value.ToStringEx() != "null" ? value.ToStringEx() : ""))
{
return false;
}
else
{
return true;
}
}
///
/// 直接格式化字符串
///
public static string FormatMe(this String source, params object[] args)
{
return string.Format(source, args);
}
#region 数据类型转换扩展方法
///
/// object 转换成string 包括为空的情况
///
///
/// 返回值不含空格
public static string ToStringEx(this object obj)
{
return obj == null ? string.Empty : obj.ToString().Trim();
}
///
/// 时间object 转换成格式化的string 包括为空的情况
///
///
///
/// 返回值不含空格
public static string TryToDateTimeToString(this object obj, string format)
{
if (obj == null)
return string.Empty;
DateTime dt;
if (DateTime.TryParse(obj.ToString(), out dt))
return dt.ToString(format);
else
return string.Empty;
}
///
/// 字符转Int
///
///
/// 成功:返回对应Int值;失败:返回0
public static int TryToInt32(this object obj)
{
int rel = 0;
if (!string.IsNullOrEmpty(obj.ToStringEx()))
{
int.TryParse(obj.ToStringEx(), out rel);
}
return rel;
}
///
/// 字符转Int64
///
///
///
public static Int64 TryToInt64(this object obj)
{
Int64 rel = 0;
if (!string.IsNullOrEmpty(obj.ToStringEx()))
{
Int64.TryParse(obj.ToStringEx(), out rel);
}
return rel;
}
///
/// 字符转DateTime
///
///
/// 成功:返回对应Int值;失败:时间初始值
public static DateTime TryToDateTime(this object obj)
{
DateTime rel = new DateTime();
if (!string.IsNullOrEmpty(obj.ToStringEx()))
{
DateTime.TryParse(obj.ToStringEx(), out rel);
}
return rel;
}
///
/// 转换成bool类型
///
///
///
public static Boolean TryToBoolean(this object obj)
{
Boolean rel = false;
if (!string.IsNullOrEmpty(obj.ToStringEx()))
{
string s = obj.ToStringEx();
if (s.Equals("true") || s.Equals("1") || s.Equals("是"))
{
rel = true;
}
else
{
Boolean.TryParse(obj.ToStringEx(), out rel);
}
}
return rel;
}
///
/// 转换特殊字符的字符串
///
///
///
public static string ToJoin(this IEnumerable obj, string separator = ",")
{
var list = obj.ToList();
if (list == null || list.Count <= 0)
return string.Empty;
return string.Join(separator, obj);
}
#endregion 数据类型转换扩展方法
}
}