[C#] 纯文本查看 复制代码
#region UTF8转换
public static string ConvertUTF8(string strSource)
{
UTF8Encoding utf8 = new UTF8Encoding();
Byte[] encodedBytes = utf8.GetBytes(strSource);
string strDecoded = utf8.GetString(encodedBytes);
return strDecoded;
}
#endregion
#region 私有构成函数
private BasicStringHelper()
{ }
#endregion
#region 过滤Sql语句属性列字符串
/// <summary>
///
/// </summary>
/// <param name="intaItemIndex"></param>
/// <param name="strSql"></param>
/// <returns></returns>
public static string FilterTSql(int[] intaItemIndex, string strSql)
{
string strFilterResult = string.Empty;
if (intaItemIndex != null && strSql != null && intaItemIndex.Length > 0 && strSql.Length > 0)
{
string strPreFix = "select", strSuffix = "from";
string strItem = strSql.Substring(strSql.IndexOf(strPreFix) + strPreFix.Length, strSql.IndexOf(strSuffix) - strPreFix.Length);
string[] straItem = strItem.Split(',');
//strResult = straDataItem.ToString();
foreach (int ItemIndex in intaItemIndex)
{
if (ItemIndex <= straItem.Length)
{
strFilterResult += string.Format("{0},", straItem[ItemIndex - 1]);
}
}
strFilterResult = strFilterResult.Substring(0, strFilterResult.LastIndexOf(',')).Trim();
strFilterResult = string.Format("{0} {1} {2}", strPreFix, strFilterResult, strSql.Substring(strSql.IndexOf(strSuffix)));
}
else
{
strFilterResult = strSql;
}
return strFilterResult;
}
/// <summary>
///
/// </summary>
/// <param name="intaItemIndex"></param>
/// <param name="strSql"></param>
/// <param name="strPreFix"></param>
/// <returns></returns>
public static string FilterTSql(int[] intaItemIndex, string strSql, string strPreFix)
{
string strFilterResult = string.Empty;
if (intaItemIndex != null && intaItemIndex.Length > 0 && strSql.Length > 0)
{
string strSuffix = "from";
string strItem = strSql.Substring(strSql.IndexOf(strPreFix) + strPreFix.Length, strSql.IndexOf(strSuffix) - strPreFix.Length);
string[] straItem = strItem.Split(',');
//strResult = straDataItem.ToString();
foreach (int ItemIndex in intaItemIndex)
{
if (ItemIndex <= straItem.Length)
{
strFilterResult += string.Format("{0},", straItem[ItemIndex - 1]);
}
}
strFilterResult = strFilterResult.Substring(0, strFilterResult.LastIndexOf(','));
}
else
{
strFilterResult = strSql;
}
return strFilterResult;
}
#endregion
#region 字符串包含指定字符数量
/// <summary>
/// 得到字符串包含指定字符数量
/// </summary>
/// <param name="strOriginalstring">原始字符串</param>
/// <param name="strAssignString">指定字符串</param>
/// <returns>整型,自定字符数量</returns>
public static int GetAssignStringCount(string strOriginalstring, string strAssignString)
{
return Regex.Matches(strOriginalstring, strAssignString).Count;
}
#endregion
#region 验证是否为整数值
/// <summary>
/// 验证是否为整数值
/// </summary>
/// <param name="strRawData">待验证字符串</param>
/// <returns>布尔结果:true为整数,false为非整数值</returns>
public static bool CheckInteger(string strRawData)
{
bool checkSign = true;
for (int iCnt = 0; iCnt < strRawData.Length; iCnt++)
{
if (!char.IsDigit(strRawData[iCnt]))
{
checkSign = false;
iCnt = strRawData.Length;
}
}
return checkSign;
}
#endregion
#region 当前时间组合字符串
/// <summary>
/// 获取时间时间组合字符串
/// </summary>
/// <returns>时间组合字符串</returns>
public static string GetNowDateTimeString(string strPrefix)
{
DateTime dtNowTime = DateTime.Now;
return strPrefix + dtNowTime.Year.ToString() + (dtNowTime.Month.ToString().Length == 2 ? dtNowTime.Month.ToString() : ("0" + dtNowTime.Month.ToString())) + (dtNowTime.Day.ToString().Length == 2 ? dtNowTime.Day.ToString() : ("0" + dtNowTime.Day.ToString())) + (dtNowTime.Hour.ToString().Length == 2 ? dtNowTime.Hour.ToString() : ("0" + dtNowTime.Hour.ToString())) + (dtNowTime.Minute.ToString().Length == 2 ? dtNowTime.Minute.ToString() : ("0" + dtNowTime.Minute.ToString()));
}
#endregion
#region 日期格式化
/// <summary>
/// 使用指定的格式来格式化日期(使用例子:DateTimeToString(dtLastUpdate, BasicType.DateTimeFormater.dtfLongDateTime))
/// </summary>
/// <param name="ADateTime">待转格式化的时间字符串</param>
/// <param name="AFormater">指定的格式类型</param>
/// <returns></returns>
public static string DateTimeToString(DateTime ADateTime, BasicType.DateTimeFormater AFormater)
{
string strResult = "";
if (AFormater != BasicType.DateTimeFormater.dtfTime)
strResult += ADateTime.Year.ToString("0000") + "-" + ADateTime.Month.ToString("00") + "-" + ADateTime.Day.ToString("00");
if (AFormater != BasicType.DateTimeFormater.dtfDate)
strResult += ADateTime.Hour.ToString("00") + ":" + ADateTime.Minute.ToString("00") + ":" + ADateTime.Second.ToString("00");
if (AFormater == BasicType.DateTimeFormater.dtfLongDateTime)
strResult += "." + ADateTime.Millisecond.ToString("000");
if (ADateTime == DateTime.MinValue)
return strResult.Trim().Replace("0001-01-01", "1900-01-01");
else
return strResult.Trim();
}
/// <summary>
/// 使用指定的格式来格式化日期(使用例子:DateTimeToString(dtLastUpdate, BasicType.DateTimeFormater.dtfLongDateTime))
/// </summary>
/// <param name="ADateTime">待转格式化的时间字符串</param>
/// <param name="AFormater">指定的格式类型</param>
/// <returns></returns>
public static string DateTimeToString(BasicType.DateTimeFormater AFormater)
{
string strResult = "";
DateTime ADateTime = DateTime.Now;
if (AFormater != BasicType.DateTimeFormater.dtfTime)
strResult += ADateTime.Year.ToString("0000") + "-" + ADateTime.Month.ToString("00") + "-" + ADateTime.Day.ToString("00");
if (AFormater != BasicType.DateTimeFormater.dtfDate)
strResult += ADateTime.Hour.ToString("00") + ":" + ADateTime.Minute.ToString("00") + ":" + ADateTime.Second.ToString("00");
if (AFormater == BasicType.DateTimeFormater.dtfLongDateTime)
strResult += "." + ADateTime.Millisecond.ToString("000");
if (ADateTime == DateTime.MinValue)
return strResult.Trim().Replace("0001-01-01", "1900-01-01");
else
return strResult.Trim();
}
#endregion
#region 生成0-z随机字符串
/// <summary>
/// 获取0-z随即字符串
/// </summary>
/// <param name="stringLength">字符串长度</param>
/// <returns>随即字符串</returns>
public static string GetRandomNumberLetterString(int stringLength)
{
System.Text.StringBuilder strReturn = new System.Text.StringBuilder();
Random rd = new Random();
while (strReturn.Length < stringLength)
{
char c = Convert.ToChar(rd.Next((byte)'0', (byte)'z'));
if (char.IsDigit(c) || char.IsLower(c) || char.IsUpper(c))
{
strReturn.Append(c);
}
}
return strReturn.ToString().Trim();
}
#endregion
#region 生成中文字符串
/// <summary>
/// 获取指定长度中文字符串
/// </summary>
/// <param name="length">字符串长度</param>
/// <returns>中文字符串</returns>
public static string GetChineseString(int length)
{
Encoding edGB = Encoding.GetEncoding("gb2312");
object[] bytes = CreateString(length);
//根据汉字字节解码出中文汉字
string strChineseString = string.Empty;
for (int i = 0; i < length; i++)
{
strChineseString += edGB.GetString((byte[])Convert.ChangeType(bytes[i], typeof(byte[])));
}
return strChineseString;
}
private static object[] CreateString(int strlength)
{
//定义一个数组存储汉字编码的组成元素
string[] strArray = new string[16] { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f" };
Random rdObject = new Random(); //定义一个随机数对象
object[] objBytes = new object[strlength];
for (int i = 0; i < strlength; i++)
{
//获取区位码第一位
int intAreaOne = rdObject.Next(11, 14);
string strAreaOne = strArray[intAreaOne].Trim();
//获取区位码第二位并防止数据重复
rdObject = new Random(intAreaOne * unchecked((int)DateTime.Now.Ticks) + i);
int intAreaTwo;
if (intAreaOne == 13)
{
intAreaTwo = rdObject.Next(0, 7);
}
else
{
intAreaTwo = rdObject.Next(0, 16);
}
string strAreaTwo = strArray[intAreaTwo].Trim();
//获取区位码第三位
rdObject = new Random(intAreaTwo * unchecked((int)DateTime.Now.Ticks) + i);
int intAreaThree = rdObject.Next(10, 16);
string strAreaThree = strArray[intAreaThree].Trim();
//获取区位码第四位
rdObject = new Random(intAreaThree * unchecked((int)DateTime.Now.Ticks) + i);
int intAreaFore;
if (intAreaThree == 10)
{
intAreaFore = rdObject.Next(1, 16);
}
else if (intAreaThree == 15)
{
intAreaFore = rdObject.Next(0, 15);
}
else
{
intAreaFore = rdObject.Next(0, 16);
}
string str4 = strArray[intAreaFore].Trim();
//定义字节变量存储产生的随机汉字区位码
byte byte1 = Convert.ToByte(strAreaOne + strAreaTwo, 16);
byte byte2 = Convert.ToByte(strAreaThree + str4, 16);
byte[] stradd = new byte[] { byte1, byte2 };
//将产生的汉字字节放入数组
objBytes.SetValue(stradd, i);
}
return objBytes;
}
#endregion
#region 生成特定符号分隔的字符串
/// <summary>
/// 生成特定符号分隔的字符串
/// </summary>
/// <param name="strDString">待处理字符串</param>
/// <param name="strSign">分隔符</param>
/// <returns>特定符号分隔的字符串</returns>
public static string GetDivisionString(string strDString, string strSign)
{
string strResult = string.Empty;
for (int i = 0; i < strDString.Length - 2; i++)
{
strResult += strDString.Substring(i, 1) + ",";
}
strResult += strDString.Substring(strDString.Length - 1, 1);
return strResult;
}
#endregion
#region 字符串转默认整数
/// <summary>
/// 将字符串转换成带默认值整数
/// </summary>
/// <param name="AIntString">待转换字符串</param>
/// <param name="AIntDef">默认整数</param>
/// <returns>整数</returns>
public static int StringToIntDefault(string AIntString, int AIntDef)
{
try
{
return int.Parse(AIntString + "");
}
catch
{
return AIntDef;
}
}
#endregion
#region 字符串转默认浮点数
/// <summary>
/// 将字符串转换成带默认值浮点数
/// </summary>
/// <param name="AFloatString">待转换字符串</param>
/// <param name="AFloatDef">默认浮点数</param>
/// <returns>浮点数</returns>
public static float StringToFloatDefault(string AFloatString, float AFloatDef)
{
try
{
return float.Parse(AFloatString + "");
}
catch
{
return AFloatDef;
}
}
#endregion
#region 字符串转默认日期
/// <summary>
/// 将字符串转换成带默认值日期(使用例子: StringToDateTimeDef("2011-07-01 09:12:07", DateTime.MinValue))
/// </summary>
/// <param name="ADateTimeString">规正的时间字符串</param>
/// <param name="ADateTimeDef">默认日期</param>
/// <returns></returns>
public static DateTime StringToDateTimeDefault(string ADateTimeString, DateTime ADateTimeDef)
{
try
{
return DateTime.Parse(ADateTimeString + "");
}
catch
{
return ADateTimeDef;
}
}
#endregion
#region 生成GUID字符串 16位、19位、32位
/// <summary>
/// 生成GUID(32位)字符串
/// </summary>
/// <returns>GUID(32位)字符串</returns>
public static string GenerateGUIDString()
{
return Guid.NewGuid().ToString().ToUpper().Replace("-", "");
}
/// <summary>
/// 生成追加前缀“?-”的GUID(32位)字符串
/// </summary>
/// <param name="strTempType">GUID字符串前缀类型</param>
/// <returns>追加前缀的GUID(32位)字符串</returns>
public static string GenerateGUIDString(string strTempType)
{
return string.Format("{0}-{1}", strTempType, GenerateGUIDString());
}
/// <summary>
/// 生成GUID(16位)字符串
/// </summary>
/// <returns>GUID(16位)字符串</returns>
public static string GenerateGUIDTo16String()
{
long i = 1;
foreach (byte b in Guid.NewGuid().ToByteArray())
i *= ((int)b + 1);
return string.Format("{0:x}", i - DateTime.Now.Ticks);
}
/// <summary>
/// 生成追加前缀“?-”的GUID(16位)字符串
/// </summary>
/// <param name="strTempType">GUID字符串前缀类型</param>
/// <returns>追加前缀的GUID(16位)字符串</returns>
public static string GenerateGUIDTo16String(string strTempType)
{
return string.Format("{0}-{1}", strTempType, GenerateGUIDTo16String());
}
/// <summary>
/// 生成GUID(19位)的唯一字符串
/// </summary>
/// <returns>GUID(19位)字符串</returns>
public static string GenerateGUIDTo19String()
{
byte[] buffer = Guid.NewGuid().ToByteArray();
return BitConverter.ToInt64(buffer, 0).ToString();
}
/// <summary>
/// 生成追加前缀“?-”的GUID(19位)字符串
/// </summary>
/// <param name="strTempType">GUID字符串前缀类型</param>
/// <returns>追加前缀的GUID(19位)字符串</returns>
public static string GenerateGUIDTo19String(string strTempType)
{
return string.Format("{0}-{1}", strTempType, GenerateGUIDTo19String());
}
#endregion
#region 中英文字符串截取
/// <summary>
/// 截取中英文字符串为指定长度
/// </summary>
/// <param name="strOriginal">要截取的字符串</param>
/// <param name="intCutLength">截取长度</param>
/// <returns>返回截取后的字符串</returns>
public static string CutEnCnString(string strOriginal, int intCutLength)
{
Regex regCode = new Regex("[\u4e00-\u9fa5]+", RegexOptions.Compiled);
char[] chrString = strOriginal.ToCharArray();
StringBuilder sbString = new StringBuilder();
int nLength = 0;
bool isCut = false;
for (int i = 0; i < chrString.Length; i++)
{
if (regCode.IsMatch((chrString[i]).ToString()))
{
sbString.Append(chrString[i]);
nLength += 2;
}
else
{
sbString.Append(chrString[i]);
nLength = nLength + 1;
}
if (nLength > intCutLength)
{
isCut = true;
break;
}
}
if (isCut)
return sbString.ToString() + "...";
else
return sbString.ToString();
}
public static string SubString(string strCode, int intLength, bool bitSign)
{
Regex regex = new Regex("[\u4e00-\u9fa5]+", RegexOptions.Compiled);
char[] stringChar = strCode.ToCharArray();
StringBuilder sb = new StringBuilder();
int nLength = 0;
bool bitCut = false;
for (int i = 0; i < stringChar.Length; i++)
{
if (regex.IsMatch((stringChar[i]).ToString()))
{
sb.Append(stringChar[i]);
nLength += 2;
}
else
{
sb.Append(stringChar[i]);
nLength = nLength + 1;
}
if (nLength > intLength)
{
bitCut = true;
break;
}
}
if (bitCut && bitSign)
return sb.ToString() + "...";
else
return sb.ToString();
}
#endregion
#region 截取左侧字符串
/// <summary>
/// 截取左侧1位字符(默认截取一位)
/// </summary>
/// <param name="strRaw">待处理字符串</param>
/// <returns>截取后字符串</returns>
public static string CutLeftString(string strRaw)
{
return CutLeftString(strRaw, 1);
}
/// <summary>
/// 截取指定长度字符串
/// </summary>
/// <param name="strRaw">待处理字符串</param>
/// <param name="intLength">截取字符串长度</param>
/// <returns>截取后字符串</returns>
public static string CutLeftString(string strRaw, int intLength)
{
if (strRaw.Length < intLength)
{
intLength = strRaw.Length;
}
return strRaw.Substring(0, intLength);
}
#endregion
#region 截取右侧字符串
/// <summary>
/// 截取右侧1位字符串(默认截取一位)
/// </summary>
/// <param name="strRaw">待处理字符串</param>
/// <returns>截取后字符串</returns>
public static string CutRightString(string strRaw)
{
return CutRightString(strRaw, 1);
}
/// <summary>
/// 截取右侧指定长度字符串
/// </summary>
/// <param name="strRaw">待处理字符串</param>
/// <param name="intLength">指定长度</param>
/// <returns>截取后字符串</returns>
public static string CutRightString(string strRaw, int intLength)
{
if (strRaw.Length < intLength)
{
intLength = strRaw.Length;
}
return strRaw.Substring(strRaw.Length - intLength, intLength);
}
#endregion
#region 创建重复字符串
/// <summary>
/// 创建重复字符串
/// </summary>
/// <param name="strDuplicateElement">重复字符串元素</param>
/// <param name="intDuplicateCount">重复计次</param>
/// <returns>生成的重复字符串</returns>
public static string CreateDuplicateString(string strDuplicateElement, int intDuplicateCount)
{
StringBuilder sbDuplicate = new StringBuilder();
for (int intSign = 0; intSign < intDuplicateCount; intSign++)
{
sbDuplicate.Append(strDuplicateElement);
}
return sbDuplicate.ToString();
}
#endregion
#region 指定字符填充字符串左侧不足长度的位置
/// <summary>
/// 指定字符填充指定长度字符串左侧不足长度的位置(默认填充“0”)
/// </summary>
/// <param name="strRaw">待处理字符串</param>
/// <param name="intLength">字符串长度</param>
/// <returns>填充后字符串</returns>
public static string FillLeftString(string strRaw, int intLength)
{
return FillLeftString(strRaw, intLength, "0");
}
/// <summary>
/// 指定字符填充指定长度字符串左侧不足长度的位置
/// </summary>
/// <param name="strRaw">待处理字符串</param>
/// <param name="intLength">字符串长度</param>
/// <param name="strRawElement">指定填充字符(为一个字节元,大于一个将自动选择第一个字节元)</param>
/// <returns>填充后字符串</returns>
public static string FillLeftString(string strRaw, int intLength, string strRawElement)
{
string strFill = string.Empty;
if (strRawElement.Length > 1)
{
strRawElement = strRawElement.Substring(0, 1);
}
if (strRaw.Length >= intLength)
{
strFill = strRaw;
}
else
{
strFill = CreateDuplicateString(strRawElement, intLength - strRaw.Length) + strRaw;
}
return strFill;
}
#endregion
#region 指定字符填充字符串右侧不足长度的位置
/// <summary>
/// 指定字符填充指定长度字符串右侧不足长度的位置(默认填充“0”)
/// </summary>
/// <param name="strRaw">待处理字符串</param>
/// <param name="intLength">字符串长度</param>
/// <returns>填充后字符串</returns>
public static string FillRightString(string strRaw, int intLength)
{
return FillRightString(strRaw, intLength, "0");
}
/// <summary>
/// 指定字符填充指定长度字符串右侧不足长度的位置
/// </summary>
/// <param name="strRaw">待处理字符串</param>
/// <param name="intLength">字符串长度</param>
/// <param name="strRawElement">指定填充字符(为一个字节元,大于一个将自动选择第一个字节元)</param>
/// <returns>填充后字符串</returns>
public static string FillRightString(string strRaw, int intLength, string strRawElement)
{
string strFill = string.Empty;
if (strRawElement.Length > 1)
{
strRawElement = strRawElement.Substring(0, 1);
}
if (strRaw.Length >= intLength)
{
strFill = strRaw;
}
else
{
strFill = strRaw + CreateDuplicateString(strRawElement, intLength - strRaw.Length);
}
return strFill;
}
#endregion
#region 个位阿拉伯数字转中文数字(零、一、二、……)
/// <summary>
/// 中文数字
/// </summary>
private static string[] strChineseArray = { "零", "一", "二", "三", "四", "五", "六", "七", "八", "九" };
/// <summary>
/// 个位阿拉伯数字转中文数字(零、一、二、……)
/// </summary>
/// <param name="intRaw">一位阿拉伯数字(0-9)</param>
/// <returns>中文数字</returns>
public static string GetBitsNumberToChinese(int intRaw)
{
string strResultChinese = string.Empty;
int intCount = -1;
foreach (string strChinese in strChineseArray)
{
intCount++;
if (intCount == intRaw)
{
strResultChinese = strChinese;
break;
}
}
return strResultChinese;
}
/// <summary>
/// 个位阿拉伯数字转中文数字(零、一、二、……)
/// </summary>
/// <param name="strRaw">字符串类型一位阿拉伯数字(“0”-“9”)</param>
/// <returns>中文数字</returns>
public static string GetBitsNumberToChinese(string strRaw)
{
return GetBitsNumberToChinese(Convert.ToInt32(strRaw));
}
#endregion
#region 个位阿拉伯数字转中文大写(零、壹、贰、……)
/// <summary>
/// 中文大写
/// </summary>
private static string[] strChineseCapitalArray = { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" };
/// <summary>
/// 个位阿拉伯数字转中文大写(零、壹、贰、……)
/// </summary>
/// <param name="intRaw">一位阿拉伯数字(0-9)</param>
/// <returns>中文大写</returns>
public static string GetBitsNumberToChineseCapital(int intRaw)
{
string strResultChineseCapital = string.Empty;
int intCount = -1;
foreach (string strChineseCapital in strChineseCapitalArray)
{
intCount++;
if (intCount == intRaw)
{
strResultChineseCapital = strChineseCapital;
break;
}
}
return strResultChineseCapital;
}
/// <summary>
/// 个位阿拉伯数字转中文大写(零、壹、贰、……)
/// </summary>
/// <param name="strRaw">字符串类型一位阿拉伯数字(“0”-“9”)</param>
/// <returns>中文大写</returns>
public static string GetBitsNumberToChineseCapital(string strRaw)
{
return GetBitsNumberToChineseCapital(Convert.ToInt32(strRaw));
}
#endregion
#region 取得每四位阿拉伯数字的中文数字(万、亿、兆、京、……)
/// <summary>
/// 每四位数的中文数字
/// </summary>
private static string[] strEveryFourDigitsChineseArray = { "", "万", "亿", "兆", "京", "垓", "秭", "穰", "沟", "涧", "正", "载", "极" };
/// <summary>
/// 取得每四位阿拉伯数字的中文数字(万、亿、兆、京、……)
/// </summary>
/// <param name="intAnyFourDigitsSign">第n(0-12)个四位数</param>
/// <returns></returns>
public static string GetEveryFourDigitsChinese(int intAnyFourDigitsSign)
{
string strEveryFourChinese = "∞";
int intCount = -1;
foreach (string strEveryFourDigitsChinese in strEveryFourDigitsChineseArray)
{
intCount++;
if (intCount == intAnyFourDigitsSign)
{
strEveryFourChinese = strEveryFourDigitsChinese;
break;
}
}
return strEveryFourChinese;
}
#endregion
#region 整数转中文数字(10050 => 一万零五十)
/// <summary>
/// 整数转中文数字(10050 => 一万零五十)
/// </summary>
/// <param name="intRawNumber">待处理整数(int)</param>
/// <returns>中文数字字符串</returns>
public static string GetNumberToChinese(int intRawNumber)
{
return GetNumberToChinese(intRawNumber.ToString());
}
/// <summary>
/// 整数转中文数字(10050 => 一万零五十)
/// </summary>
/// <param name="intRawNumber">待处理整数(Int64)</param>
/// <returns>中文数字字符串</returns>
public static string GetNumberToChinese(Int64 intRawNumber)
{
return GetNumberToChinese(intRawNumber.ToString());
}
/// <summary>
/// 整数转中文数字(10050 => 一万零五十)
/// </summary>
/// <param name="intRawNumber">待处理整数(UInt64)</param>
/// <returns>中文数字字符串</returns>
public static string GetNumberToChinese(UInt64 intRawNumber)
{
return GetNumberToChinese(intRawNumber.ToString());
}
/// <summary>
/// 整数字符串转中文数字(10050 => 一万零五十)
/// </summary>
/// <param name="strRawNumber">待处理整数字符串</param>
/// <returns>中文数字字符串</returns>
public static string GetNumberToChinese(string strRawNumber)
{
int intRawLength = 0, intSign = 0, intAnyFourSign = -1;
string strChinese = string.Empty, strReturnChinese = string.Empty, strTempChinese = string.Empty;
//检测是否为整数
if (!CheckInteger(strRawNumber))
{
strRawNumber = "0";
}
intRawLength = strRawNumber.Length;
//strRawNumber归一化(四位为一个处理单位)
intSign = intRawLength % 4;
strRawNumber = CreateDuplicateString("0", 4 - intSign) + strRawNumber;
intRawLength = strRawNumber.Length;
for (int intFourSign = 0; intFourSign < intRawLength; intFourSign += 4)
{
intAnyFourSign++;
strChinese = string.Empty;
for (int intEverySign = 0; intEverySign < 4; intEverySign++)
{
int intBitsSign = intRawLength - intFourSign - intEverySign - 1;
strTempChinese = GetBitsNumberToChinese(strRawNumber.Substring(intBitsSign, 1));
if (string.Equals(strTempChinese, "零"))
{
if (!string.Equals(CutLeftString(strChinese, 1), "零") & !string.IsNullOrEmpty(CutLeftString(strChinese, 1)))
{
strChinese = strTempChinese + strChinese;
}
}
else
{
switch (intEverySign)
{
case 0:
strChinese = strTempChinese;
break;
case 1:
strChinese = string.Format("{0}十{1}", strTempChinese, strChinese);
break;
case 2:
strChinese = string.Format("{0}百{1}", strTempChinese, strChinese);
break;
case 3:
strChinese = string.Format("{0}千{1}", strTempChinese, strChinese);
break;
default:
strChinese = string.Format("{0}∞{1}", strTempChinese, strChinese);
break;
}
}
}
if (string.Equals(strChinese, "零") & !string.IsNullOrEmpty(CutLeftString(strReturnChinese, 1)) & !string.Equals(CutLeftString(strReturnChinese, 1), "零"))
{
strReturnChinese = string.Format("零{0}", strReturnChinese);
}
else
{
if (!string.IsNullOrEmpty(strChinese))
{
strReturnChinese = strChinese + GetEveryFourDigitsChinese(intAnyFourSign) + strReturnChinese;
}
}
}
if (string.IsNullOrEmpty(strReturnChinese))
{
strReturnChinese = "零";
}
else if (string.Equals(CutLeftString(strReturnChinese, 1), "零"))
{
strReturnChinese = strReturnChinese.Substring(1, strReturnChinese.Length - 1);
}
return strReturnChinese;
}
#endregion
#region 整数转中文数字(10050 => 一万零千零百五十零)
/// <summary>
/// 整数转中文数字(10050 => 一万零千零百五十零)
/// </summary>
/// <param name="intRawNumber">待处理整数(int)</param>
/// <returns>中文数字字符串</returns>
public static string GetNumberToFillChinese(int intRawNumber)
{
return GetNumberToFillChinese(intRawNumber.ToString());
}
/// <summary>
/// 整数转中文数字(10050 => 一万零千零百五十零)
/// </summary>
/// <param name="intRawNumber">待处理整数(Int64)</param>
/// <returns>中文数字字符串</returns>
public static string GetNumberToFillChinese(Int64 intRawNumber)
{
return GetNumberToFillChinese(intRawNumber.ToString());
}
/// <summary>
/// 整数转中文数字(10050 => 一万零千零百五十零)
/// </summary>
/// <param name="intRawNumber">待处理整数(UInt64)</param>
/// <returns>中文数字字符串</returns>
public static string GetNumberToFillChinese(UInt64 intRawNumber)
{
return GetNumberToFillChinese(intRawNumber.ToString());
}
/// <summary>
/// 整数字符串转中文数字(10050 => 一万零千零百五十零)
/// </summary>
/// <param name="strRawNumber">待处理整数字符串</param>
/// <returns>中文数字字符串</returns>
public static string GetNumberToFillChinese(string strRawNumber)
{
int intRawLength = 0, intSign = 0, intAnyFourSign = -1;
string strChinese = string.Empty, strReturnChinese = string.Empty, strTempChinese = string.Empty;
//检测是否为整数
if (!CheckInteger(strRawNumber))
{
strRawNumber = "0";
}
intRawLength = strRawNumber.Length;
//strRawNumber归一化(四位为一个处理单位)
intSign = intRawLength % 4;
strRawNumber = CreateDuplicateString("0", 4 - intSign) + strRawNumber;
intRawLength = strRawNumber.Length;
for (int intFourSign = 0; intFourSign < intRawLength; intFourSign += 4)
{
intAnyFourSign++;
strChinese = string.Empty;
for (int intEverySign = 0; intEverySign < 4; intEverySign++)
{
int intBitsSign = intRawLength - intFourSign - intEverySign - 1;
strTempChinese = GetBitsNumberToChinese(strRawNumber.Substring(intBitsSign, 1));
switch (intEverySign)
{
case 0:
strChinese = strTempChinese;
break;
case 1:
strChinese = string.Format("{0}十{1}", strTempChinese, strChinese);
break;
case 2:
strChinese = string.Format("{0}百{1}", strTempChinese, strChinese);
break;
case 3:
strChinese = string.Format("{0}千{1}", strTempChinese, strChinese);
break;
default:
strChinese = string.Format("{0}∞{1}", strTempChinese, strChinese);
break;
}
}
if (!string.IsNullOrEmpty(strChinese))
{
strReturnChinese = strChinese + GetEveryFourDigitsChinese(intAnyFourSign) + strReturnChinese;
}
}
while (string.Equals(CutLeftString(strReturnChinese, 1), "零") & strReturnChinese.Length > 1)
{
if (string.Equals(CutLeftString(strReturnChinese, 1), "零"))
{
strReturnChinese = strReturnChinese.Substring(2, strReturnChinese.Length - 2);
}
}
return strReturnChinese;
}
#endregion
#region 整数转简体中文数字(10050 => 一零零五零)
/// <summary>
/// 整数转简体中文数字(10050 => 一零零五零)
/// </summary>
/// <param name="intRawNumber">待处理整数(int)</param>
/// <returns>中文数字字符串</returns>
public string GetNumberShortChinese(int intRawNumber)
{
return GetNumberShortChinese(intRawNumber.ToString());
}
/// <summary>
/// 整数转简体中文数字(10050 => 一零零五零)
/// </summary>
/// <param name="intRawNumber">待处理整数(Int64)</param>
/// <returns>中文数字字符串</returns>
public string GetNumberShortChinese(Int64 intRawNumber)
{
return GetNumberShortChinese(intRawNumber.ToString());
}
/// <summary>
/// 整数转简体中文数字(10050 => 一零零五零)
/// </summary>
/// <param name="intRawNumber">待处理整数(UInt64)</param>
/// <returns>中文数字字符串</returns>
public string GetNumberShortChinese(UInt64 intRawNumber)
{
return GetNumberShortChinese(intRawNumber.ToString());
}
/// <summary>
/// 整数转简体中文数字(10050 => 一零零五零)
/// </summary>
/// <param name="strRawNumber">待处理整数字符串</param>
/// <returns>中文数字字符串</returns>
public string GetNumberShortChinese(string strRawNumber)
{
string strShortChinese = string.Empty;
if (!CheckInteger(strRawNumber))
{
strRawNumber = "0";
}
int intRawLength = strRawNumber.Length;
for (int intSign = 0; intSign < intRawLength; intSign++)
{
strShortChinese += GetBitsNumberToChinese(strRawNumber[intSign].ToString());
}
return strShortChinese;
}
#endregion
#region 整数转中文大写数字(10050 => 壹万零伍拾)
/// <summary>
/// 整数转中文大写数字(10050 => 壹万零伍拾)
/// </summary>
/// <param name="strRawNumber">待处理整数(int)</param>
/// <returns>中文数字大写字符串</returns>
public string GetNumberToChineseCapital(int intRawNumber)
{
return GetNumberToChineseCapital(intRawNumber.ToString());
}
/// <summary>
/// 整数转中文大写数字(10050 => 壹万零伍拾)
/// </summary>
/// <param name="strRawNumber">待处理整数(Int64)</param>
/// <returns>中文数字大写字符串</returns>
public string GetNumberToChineseCapital(Int64 intRawNumber)
{
return GetNumberToChineseCapital(intRawNumber.ToString());
}
/// <summary>
/// 整数转中文大写数字(10050 => 壹万零伍拾)
/// </summary>
/// <param name="strRawNumber">待处理整数(UInt64)</param>
/// <returns>中文数字大写字符串</returns>
public string GetNumberToChineseCapital(UInt64 intRawNumber)
{
return GetNumberToChineseCapital(intRawNumber.ToString());
}
/// <summary>
/// 整数转中文大写数字(10050 => 壹万零伍拾)
/// </summary>
/// <param name="strRawNumber">待处理整数字符串</param>
/// <returns>中文数字大写字符串</returns>
public string GetNumberToChineseCapital(string strRawNumber)
{
int intRawLength = 0, intSign = 0, intAnyFourSign = -1;
string strChineseCapital = string.Empty, strResultChineseCapital = string.Empty, strTempChinese = string.Empty;
//检测是否为整数
if (!CheckInteger(strRawNumber))
{
strRawNumber = "0";
}
intRawLength = strRawNumber.Length;
//strRawNumber归一化(四位为一个处理单位)
intSign = intRawLength % 4;
strRawNumber = CreateDuplicateString("0", 4 - intSign) + strRawNumber;
intRawLength = strRawNumber.Length;
for (int intFourSign = 0; intFourSign < intRawLength; intFourSign += 4)
{
intAnyFourSign++;
strChineseCapital = string.Empty;
for (int intEverySign = 0; intEverySign < 4; intEverySign++)
{
int intBitsSign = intRawLength - intFourSign - intEverySign - 1;
strTempChinese = GetBitsNumberToChineseCapital(strRawNumber.Substring(intBitsSign, 1));
if (string.Equals(strTempChinese, "零"))
{
if (!string.Equals(CutLeftString(strChineseCapital, 1), "零") & !string.IsNullOrEmpty(CutLeftString(strChineseCapital, 1)))
{
strChineseCapital = strTempChinese + strChineseCapital;
}
}
else
{
switch (intEverySign)
{
case 0:
strChineseCapital = strTempChinese;
break;
case 1:
strChineseCapital = string.Format("{0}拾{1}", strTempChinese, strChineseCapital);
break;
case 2:
strChineseCapital = string.Format("{0}佰{1}", strTempChinese, strChineseCapital);
break;
case 3:
strChineseCapital = string.Format("{0}仟{1}", strTempChinese, strChineseCapital);
break;
default:
strChineseCapital = string.Format("{0}{1}", strTempChinese, strChineseCapital);
break;
}
}
}
if (string.Equals(strChineseCapital, "零") & !string.IsNullOrEmpty(CutLeftString(strResultChineseCapital, 1)) & !string.Equals(CutLeftString(strResultChineseCapital, 1), "零"))
{
strResultChineseCapital = string.Format("零{0}", strResultChineseCapital);
}
else
{
if (!string.IsNullOrEmpty(strChineseCapital))
{
strResultChineseCapital = strChineseCapital + GetEveryFourDigitsChinese(intAnyFourSign) + strResultChineseCapital;
}
}
}
if (string.IsNullOrEmpty(strResultChineseCapital))
{
strResultChineseCapital = "零";
}
else if (string.Equals(CutLeftString(strResultChineseCapital, 1), "零"))
{
strResultChineseCapital = strResultChineseCapital.Substring(1, strResultChineseCapital.Length - 1);
}
return strResultChineseCapital;
}
#endregion
#region 整数转中文大写数字(10050 => 壹万零仟零佰伍拾零)
/// <summary>
/// 整数转中文大写数字(10050 => 壹万零仟零佰伍拾零)
/// </summary>
/// <param name="strRawNumber">待处理整数(int)</param>
/// <returns>中文数字大写字符串</returns>
public string GetNumberToChineseCapitalFill(int intRawNumber)
{
return GetNumberToChineseCapitalFill(intRawNumber.ToString());
}
/// <summary>
/// 整数转中文大写数字(10050 => 壹万零仟零佰伍拾零)
/// </summary>
/// <param name="strRawNumber">待处理整数(Int64)</param>
/// <returns>中文数字大写字符串</returns>
public string GetNumberToChineseCapitalFill(Int64 intRawNumber)
{
return GetNumberToChineseCapitalFill(intRawNumber.ToString());
}
/// <summary>
/// 整数转中文大写数字(10050 => 壹万零仟零佰伍拾零)
/// </summary>
/// <param name="strRawNumber">待处理整数(UInt64)</param>
/// <returns>中文数字大写字符串</returns>
public string GetNumberToChineseCapitalFill(UInt64 intRawNumber)
{
return GetNumberToChineseCapitalFill(intRawNumber.ToString());
}
/// <summary>
/// 整数转中文大写数字(10050 => 壹万零仟零佰伍拾零)
/// </summary>
/// <param name="strRawNumber">待处理整数字符串</param>
/// <returns>中文数字大写字符串</returns>
public string GetNumberToChineseCapitalFill(string strRawNumber)
{
int intRawLength = 0, intSign = 0, intAnyFourSign = -1;
string strChinese = string.Empty, strResultChineseCapitalFill = string.Empty, strTempChinese = string.Empty;
//检测是否为整数
if (!CheckInteger(strRawNumber))
{
strRawNumber = "0";
}
intRawLength = strRawNumber.Length;
//strRawNumber归一化(四位为一个处理单位)
intSign = intRawLength % 4;
strRawNumber = CreateDuplicateString("0", 4 - intSign) + strRawNumber;
intRawLength = strRawNumber.Length;
for (int intFourSign = 0; intFourSign < intRawLength; intFourSign += 4)
{
intAnyFourSign++;
strChinese = string.Empty;
for (int intEverySign = 0; intEverySign < 4; intEverySign++)
{
int intBitsSign = intRawLength - intFourSign - intEverySign - 1;
strTempChinese = GetBitsNumberToChineseCapital(strRawNumber.Substring(intBitsSign, 1));
switch (intEverySign)
{
case 0:
strChinese = strTempChinese;
break;
case 1:
strChinese = string.Format("{0}拾{1}", strTempChinese, strChinese);
break;
case 2:
strChinese = string.Format("{0}佰{1}", strTempChinese, strChinese);
break;
case 3:
strChinese = string.Format("{0}仟{1}", strTempChinese, strChinese);
break;
default:
strChinese = string.Format("{0}{1}", strTempChinese, strChinese);
break;
}
}
if (!string.IsNullOrEmpty(strChinese))
{
strResultChineseCapitalFill = strChinese + GetEveryFourDigitsChinese(intAnyFourSign) + strResultChineseCapitalFill;
}
}
while (string.Equals(CutLeftString(strResultChineseCapitalFill, 1), "零") & strResultChineseCapitalFill.Length > 1)
{
if (string.Equals(CutLeftString(strResultChineseCapitalFill, 1), "零"))
{
strResultChineseCapitalFill = strResultChineseCapitalFill.Substring(2, strResultChineseCapitalFill.Length - 2);
}
}
return strResultChineseCapitalFill;
}
#endregion
#region 整数转中文大写数字(10050 => 壹零零伍零)
/// <summary>
/// 整数转中文大写数字(10050 => 壹零零伍零)
/// </summary>
/// <param name="strRawNumber">待处理整数(int)</param>
/// <returns>中文数字大写字符串</returns>
public string GetNumberToChineseCapitalShort(int intRawNumber)
{
return GetNumberToChineseCapitalShort(intRawNumber.ToString());
}
/// <summary>
/// 整数转中文大写数字(10050 => 壹零零伍零)
/// </summary>
/// <param name="strRawNumber">待处理整数(Int64)</param>
/// <returns>中文数字大写字符串</returns>
public string GetNumberToChineseCapitalShort(Int64 intRawNumber)
{
return GetNumberToChineseCapitalShort(intRawNumber.ToString());
}
/// <summary>
/// 整数转中文大写数字(10050 => 壹零零伍零)
/// </summary>
/// <param name="strRawNumber">待处理整数(UInt64)</param>
/// <returns>中文数字大写字符串</returns>
public string GetNumberToChineseCapitalShort(UInt64 intRawNumber)
{
return GetNumberToChineseCapitalShort(intRawNumber.ToString());
}
/// <summary>
/// 整数转中文大写数字(10050 => 壹零零伍零)
/// </summary>
/// <param name="strRawNumber">待处理整数字符串</param>
/// <returns>中文数字大写字符串</returns>
public string GetNumberToChineseCapitalShort(string strRawNumber)
{
string strChineseCapitalShort = string.Empty;
if (!CheckInteger(strRawNumber))
{
strRawNumber = "0";
}
int intRawLength = strRawNumber.Length;
for (int intSign = 0; intSign < intRawLength; intSign++)
{
strChineseCapitalShort += GetBitsNumberToChineseCapital(strRawNumber[intSign].ToString());
}
return strChineseCapitalShort;
}
#endregion