【c#】DataChecker工具类大全分享

IsEmpty functions

DataSet

/// <summary>
/// DataSet is null or empty
/// </summary>
/// <param name="dataSet">checking DataSet</param>
/// <returns>if null or empty, then return<c>true</c>; else return <c>false</c>.</returns>
public static bool IsEmpty(DataSet dataSet)
{
    return !(IsNotEmpty(dataSet));
}

DataTable

/// <summary>
/// DataTable is null or empty
/// </summary>
/// <param name="dataTable">DataTable.</param>
/// <returns>if null or empty, then return<c>true</c><c>true</c>;else return <c>false</c>.</returns>
public static bool IsEmpty(DataTable dataTable)
{
    return !(IsNotEmpty(dataTable));
}

String

code@1

/// <summary>
/// input string is null or String.Empty
/// </summary>
/// <param name="str">checking string</param>
/// <param name="removeSpace">remove front/end spaces or not</param>
/// <returns>if null or empty, then return<c>true</c>; else return <c>false</c>.</returns>
public static bool IsEmpty(string str, bool removeSpace)
{
    if (str == null)
    {
        return true;
    }

    if (removeSpace)
    {
        return (str.Trim().Length == 0);
    }

    return (str.Length == 0);
}

code@2

public static bool IsReadEmpty(string str)
{
    return IsRealEmpty(str, true);
}

code@3

public static bool IsRealEmpty(string str, bool removeSpace)
{
    if (str == null)
    {
        return true;
    }

    if (removeSpace)
    {
        return (str.Replace("&nbsp;", " ").Trim().Length == 0);
    }

    return (str.Length == 0);
}

ICollection

code@1

/// <summary>
/// Determines whether the specified value is empty.
/// </summary>
/// <param name="value">The value.</param>
/// <returns>
///     <c>true</c> if the specified value is empty; otherwise, <c>false</c>.
/// </returns>
public static bool IsEmpty(ICollection value)
{
    return (value == null || value.Count == 0);
}

code@2

/// <summary>
/// Determines whether the specified value is empty.
/// </summary>
/// <param name="value">The value.</param>
/// <returns>
///     <c>true</c> if the specified value is empty; otherwise, <c>false</c>.
/// </returns>
public static bool IsEmpty<T>(ICollection<T> value)
{
    return (value == null || value.Count == 0);
}

Object

/// <summary>
/// Determines whether the specified value is empty.
/// </summary>
/// <param name="value">The value.</param>
/// <returns>
///     <c>true</c> if the specified value is empty; otherwise, <c>false</c>.
/// </returns>
public static bool IsEmpty(object value)
{
    if (value == null)
    {
        return true;
    }

    if (value is string)
    {
        return IsEmpty((string)value);
    }

    if (value is ICollection)
    {
        return IsEmpty((ICollection)value);
    }

    if (value is DataSet)
    {
        return IsEmpty((DataSet)value);
    }

    if (value is DataTable)
    {
        return IsEmpty((DataTable)value);
    }

    return false;
}

IsNotEmpty functions

DataSet

/// <summary>
/// data in DataSet
/// </summary>
/// <param name="dataSet">checking DataSet</param>
/// <returns>if null or empty, then return<c>false</c>; otherwise <c>true</c>.</returns>
public static bool IsNotEmpty(DataSet dataSet)
{
    if (dataSet == null)
    {
        return false;
    }
    for (int i = 0; i < dataSet.Tables.Count; i++)
    {
        if (IsNotEmpty(dataSet.Tables[i]))
        {
            return true;
        }
    }
    return false;
}

DataTable

/// <summary>
/// data in DataTable
/// </summary>
/// <param name="dataTable">DataTable.</param>
/// <returns>if null or empty, then return<c>false</c>; else return <c>true</c>.</returns>
public static bool IsNotEmpty(DataTable dataTable)
{
    if (dataTable == null)
    {
        return false;
    }

    if (dataTable.Rows.Count > 0)
    {
        return true;
    }

    return false;
}

String

code@1

/// <summary>
/// string is null or String.Empty。
/// </summary>
/// <param name="str">checking string</param>
/// <param name="removeSpace">remove front and end spaces or not</param>
/// <returns>if null or empty, then return<c>false</c>; else return <c>true</c>.</returns>
public static bool IsNotEmpty(string str, bool removeSpace)
{
    return (!IsEmpty(str, removeSpace));
}

code@2

/// <summary>
/// input string is null or String.Empty,remove the front and end spaces.
/// </summary>
/// <param name="str">checking string</param>
/// <returns>if null or empty, then return<c>false</c>; else return <c>true</c>.</returns>
public static bool IsNotEmpty(string str)
{
    return IsNotEmpty(str, true);
}

ICollection

code@1

/// <summary>
/// Determines whether [is not empty] [the specified value].
/// </summary>
/// <param name="value">The value.</param>
/// <returns>
///     <c>true</c> if [is not empty] [the specified value]; otherwise, <c>false</c>.
/// </returns>
public static bool IsNotEmpty(ICollection value)
{
    return !IsEmpty(value);
}

code@2

/// <summary>
/// Determines whether [is not empty] [the specified value].
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="value">The value.</param>
/// <returns>
///     <c>true</c> if [is not empty] [the specified value]; otherwise, <c>false</c>.
/// </returns>
public static bool IsNotEmpty<T>(ICollection<T> value)
{
    return !IsEmpty(value);
}

Object

/// <summary>
/// Determines whether [is not empty] [the specified value].
/// </summary>
/// <param name="value">The value.</param>
/// <returns>
///     <c>true</c> if [is not empty] [the specified value]; otherwise, <c>false</c>.
/// </returns>
public static bool IsNotEmpty(object value)
{
    return !IsEmpty(value);
}

HasEmpty

/// <summary>
/// Determines whether the specified args has empty.
/// </summary>
/// <param name="args">The args.</param>
/// <returns>
///     <c>true</c> if the specified args has empty; otherwise, <c>false</c>.
/// </returns>
public static bool HasEmpty(params object[] args)
{
    foreach (object arg in args)
    {
        if (IsEmpty(arg))
        {
            return true;
        }
    }
    return false;
}

AllAreEmpty

/// <summary>
/// Alls the are empty.
/// </summary>
/// <param name="args">The args.</param>
/// <returns></returns>
public static bool AllAreEmpty(params object[] args)
{
    foreach (object arg in args)
    {
        if (IsNotEmpty(arg))
        {
            return false;
        }
    }
    return true;
}

AllAreNotEmpty

/// <summary>
/// Alls the are not empty.
/// </summary>
/// <param name="args">The args.</param>
/// <returns></returns>
public static bool AllAreNotEmpty(params object[] args)
{
    foreach (object arg in args)
    {
        if (IsEmpty(arg))
        {
            return false;
        }
    }
    return true;
}

Digit checking

IsUInt

/// <summary>
/// Decimal and unsigned data
/// </summary>
/// <param name="input">input string</param>
/// <returns></returns>
public static bool IsUInt(string input)
{
    Regex regUInt = new Regex("^[0-9]+$");

    if (!IsNotEmpty(input))
    {
        return false;
    }

    Match match = regUInt.Match(input);
    return match.Success;
}

IsInt

/// <summary>
/// Decimal integer string.
/// </summary>
/// <param name="input">input string</param>
/// <returns></returns>
public static bool IsInt(string input)
{
    Regex regInt = new Regex("^[+-]?[0-9]+$");

    if (!IsNotEmpty(input))
    {
        return false;
    }

    Match match = regInt.Match(input);
    return match.Success;
}

IsUDecimal

/// <summary>
/// Whether it is an unsigned decimal number, including decimal.
/// </summary>
/// <param name="input">input string</param>
/// <returns></returns>
public static bool IsUDecimal(string input)
{
    Regex regUDecimal = new Regex("^[0-9]+[.]?[0-9]*$");

    if (!IsNotEmpty(input))
    {
        return false;
    }

    Match match = regUDecimal.Match(input);
    return match.Success;
}

IsDecimal

/// <summary>
/// Is a decimal number or not, including decimal.
/// </summary>
/// <param name="input">input string</param>
/// <returns></returns>
public static bool IsDecimal(string input)
{
    Regex regDecimal = new Regex("^[+-]?[0-9]+[.]?[0-9]*$"); //==>  ^[+-]?\d+[.]?\d+$

    if (!IsNotEmpty(input))
    {
        return false;
    }

    Match match = regDecimal.Match(input);
    return match.Success;
}

Chinese checking

/// <summary>
/// check chinese is exist or not
/// </summary>
/// <param name="input">input string</param>
/// <returns></returns>
public static bool HasChinese(string input)
{
    Regex regChinese = new Regex("[\u4e00-\u9fa5]");

    if (!IsNotEmpty(input))
    {
        return false;
    }

    Match match = regChinese.Match(input);
    return match.Success;
}

Mail Address check

/// <summary>
/// Is a floating-point numbers, may have + and -
/// </summary>
/// <param name="input">input string</param>
/// <returns></returns>
public static bool IsEmail(string input)
{
    Regex regEmail = new Regex("^[\\w-]+@[\\w-]+\\.(com|net|org|edu|mil|tv|biz|info)$");

    if (!IsNotEmpty(input))
    {
        return false;
    }

    Match match = regEmail.Match(input);
    return match.Success;
}

MathRelative

ToSBC-转全角的函数

///转全角的函数(SBC case)
///
///任意字符串
///全角字符串
///
///全角空格为12288,半角空格为32///其他字符半角(33-126)与全角(65281-65374)的对应关系是:均相差65248
///        
public static string ToSBC(string input)
{
    //半角转全角:
    char[] c = input.ToCharArray();
    for (int i = 0; i < c.Length; i++)
    {
        if (c[i] == 32)
        {
            c[i] = (char)12288;
            continue;
        }
        if (c[i] < 127)
            c[i] = (char)(c[i] + 65248);
    }
    return new string(c);
}

ToDBC-转半角的函数

///转半角的函数(DBC case)
///
///任意字符串
///半角字符串
///
///全角空格为12288,半角空格为32
///其他字符半角(33-126)与全角(65281-65374)的对应关系是:均相差65248
///
public static string ToDBC(string input)
{
    char[] c = input.ToCharArray();
    for (int i = 0; i < c.Length; i++)
    {
        if (c[i] == 12288)
        {
            c[i] = (char)32;
            continue;
        }
        if (c[i] > 65280 && c[i] < 65375)
            c[i] = (char)(c[i] - 65248);
    }
    return new string(c);
}
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值