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;
}
}
TextEntity.cs
最新推荐文章于 2024-01-24 22:33:04 发布