TextEntity.cs

using System;
using System.IO;
using System.Text.RegularExpressions;// Regex

/// <summary>
/// TextEntity 的摘要说明
/// </summary>
public class TextEntity
{
    // 构造函数
    public TextEntity() { }

    // 判断是否为有效数字字符串
    public bool IsValidNumber(string value)
    {
        bool result = false;
        try
        {
            double.Parse(value);
            result = true;
        }
        catch
        {
        }
        return result;
    }

    // 判断是否为有效日期
    public bool IsValidDate(string date)
    {
        bool result = false;
        try
        {
            DateTime.Parse(date);
            result = true;
        }
        catch
        {
        }
        return result;
    }

    // 判断字符串是否有中文
    public bool IsHasChinese(string text)
    {
        for (int i = 0; i < text.Length; i++)
        {
            if (Convert.ToInt32(Convert.ToChar(text.Substring(i, 1))) >= Convert.ToInt32(Convert.ToChar(128)))
            {
                return true;
            }
        }
        return false;
    }

    // 获取日期格式,需要与[年/月/日]格式的数据对比,目前此函数对同月同日情况可能有误判因此请谨慎使用
    public string GetDateFormat(string date, string dateYMD)
    {
        string result = null;
        if (Regex.Match(date, "[^0-9]").ToString().Length == 0 || Regex.Match(dateYMD, "[^0-9]").ToString().Length == 0) return result;// 空值返回
        string delimit = Regex.Match(date, "[^0-9]").ToString();
        string sp1 = date.Split(new char[] { '/', '-', '.', ' ' })[0].PadLeft(2, '0');
        string sp2 = date.Split(new char[] { '/', '-', '.', ' ' })[1].PadLeft(2, '0');
        string sp3 = date.Split(new char[] { '/', '-', '.', ' ' })[2].PadLeft(2, '0');
        string year = dateYMD.Split(new char[] { '/', '-', '.', ' ' })[0].PadLeft(2, '0');
        string month = dateYMD.Split(new char[] { '/', '-', '.', ' ' })[1].PadLeft(2, '0');
        string day = dateYMD.Split(new char[] { '/', '-', '.', ' ' })[2].PadLeft(2, '0');
        if (sp1 == year && sp2 == month && sp3 == day)
        {
            result = "yyyy" + delimit + "MM" + delimit + "dd";
        }
        else if (sp1 == month && sp2 == day && sp3 == year)
        {
            result = "MM" + delimit + "dd" + delimit + "yyyy";
        }
        else if (sp1 == day && sp2 == month && sp3 == year)
        {
            result = "dd" + delimit + "MM" + delimit + "yyyy";
        }
        else if (sp1 == year.Substring(2, 2) && sp2 == month && sp3 == day)
        {
            result = "yy" + delimit + "MM" + delimit + "dd";
        }
        else if (sp1 == month && sp2 == day && sp3 == year.Substring(2, 2))
        {
            result = "MM" + delimit + "dd" + delimit + "yy";
        }
        else if (sp1 == day && sp2 == month && sp3 == year.Substring(2, 2))
        {
            result = "dd" + delimit + "MM" + delimit + "yy";
        }
        return result;
    }

    // 获取指定格式浮点数
    public string GetFloat(string value, string format)
    {
        string result = null;
        try
        {
            result = Convert.ToSingle(value).ToString(format);
        }
        catch
        {
        }
        return result;
    }

    // 获取指定格式日期
    public string GetDate(string date, string format)
    {
        string result = null;
        try
        {
            result = DateTime.Parse(date).ToString(format);
        }
        catch
        {
        }
        return result;
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值