using System;
using System.Text.RegularExpressions;
using System.Collections;
using System.Text;
using Microsoft.VisualBasic;
using System.Collections.Generic;
namespace UtilityFactory
{
/// <summary>
/// StrTools Summary Illuminate
/// </summary>
public class StringUtility
{
/// <summary>
///
/// </summary>
/// <param name="join"></param>
/// <param name="str"></param>
/// <returns></returns>
public static string Join(string join, params string[] str)
{
string value = string.Empty;
for (int i = 0; i < str.Length; i++)
{
if (!string.IsNullOrEmpty(str[i]))
{
value += join + str[i];
}
}
if (!string.IsNullOrEmpty(value))
{
value = value.Substring(join.Length);
}
return string.IsNullOrEmpty(value) ? null : value;
}
/// <summary>
/// 将数字转换成中文
/// </summary>
/// <param name="numberStr"></param>
/// <returns></returns>
public static string NumberToChinese(string numberStr)
{
string numStr = "0123456789";
string chineseStr = "零一二三四五六七八九";
char[] c = numberStr.ToCharArray();
for (int i = 0; i < c.Length; i++)
{
int index = numStr.IndexOf(c[i]);
if (index != -1)
c[i] = chineseStr.ToCharArray()[index];
}
numStr = null;
chineseStr = null;
return new string(c);
}
/// <summary>
/// 传入decimal 返回带千分位并且保留所有小数部分
/// </summary>
/// <param name="d"></param>
/// <returns></returns>
public static string DecimalWithN(decimal d)
{
if (d.ToString().IndexOf(".") > -1)
{
string strDecimal = d.ToString();
string dot = strDecimal.Substring(strDecimal.LastIndexOf("."));
if (Regex.IsMatch(dot, @"[1-9]+"))
{
//多保留一位,然后字符串截取(避免四舍五入)
strDecimal = d.ToString("N1").Substring(0 , d.ToString().IndexOf('.')) + dot;
}
else
{
strDecimal = d.ToString("N1").Substring(0, d.ToString().IndexOf('.'));
}
return strDecimal;
}
return d.ToString("N0");
}
/// <summary>
/// 计算两个数的百分比, 百分比后默认两位小数
/// </summary>
/// <param name="Dividend">被除数</param>
/// <param name="Divisor">除数</param>
/// <returns></returns>
public static string StringToPercent(string Dividend, string Divisor)
{
if (Regex.IsMatch(Dividend, @"[0-9]+") && Regex.IsMatch(Divisor, @"[0-9]+"))
{
Dividend = string.IsNullOrEmpty(Dividend) ? "0" : Dividend;
Divisor = string.IsNullOrEmpty(Divisor) ? "0" : Divisor;
decimal percent = decimal.Parse(Dividend) / decimal.Parse(Divisor);
return percent.ToString("P2");
}
return string.Empty;
}
/// <summary>
/// 输入Index 返回26个字母的顺序 最大不能超出25
/// </summary>
/// <param name="index"></param>
/// <returns></returns>
public static string NumberChangeLetter(int index)
{
ArrayList arr = new ArrayList();
for (char i = 'a'; i <= 'z'; i++)
{
arr.Add(i);
}
if (index <= 25)
{
return arr[index].ToString();
}
return string.Empty;
}
/// <summary>
/// 数字转化为英文字符串 递归算法即可
/// </summary>
/// <param name="number"></param>
/// <returns></returns>
public static string ToEnglishString(int number)
{
if (number < 0) //暂不考虑负数
{
return "";
}
if (number < 20) //0到19
{
switch (number)
{
case 0:
return "Zero";
case 1:
return "One";
case 2:
return "Two";
case 3:
return "Three";
case 4:
return "Four";
case 5:
return "Five";
case 6:
return "Six";
case 7:
return "Seven";
case 8:
return "Eight";
case 9:
return "Nine";
case 10:
return "Ten";
case 11:
return "Eleven";
case 12:
return "Twelve";
case 13:
return "Thirteen";
case 14:
return "Fourteen";
case 15:
return "Fifteen";
case 16:
return "Sixteen";
case 17:
return "Seventeen";
case 18:
return "Eighteen";
case 19:
return "Nineteen";
default:
return "";
}
}
if (number < 100) //20到99
{
if (number % 10 == 0) //20,30,40,...90的输出
{
switch (number)
{
case 20:
return "Twenty";
case 30:
return "Thirty";
case 40:
return "Forty";
case 50:
return "Fifty";
case 60:
return "Sixty";
case 70:
return "Seventy";
case 80:
return "Eighty";
case 90:
return "Ninety";
default:
return "";
}
}
else //21.22,....99 思路:26=20+6
{
return string.Format("{0} {1}", ToEnglishString(10 * (number / 10)), ToEnglishString(number % 10));
}
}
if (number < 1000) //100到999 百级
{
if (number % 100 == 0)
{
return string.Format("{0} Hundred", ToEnglishString(number / 100));
}
else
{
return string.Format("{0} Hundred and {1}", ToEnglishString(number / 100), ToEnglishString(number % 100));
}
}
if (number < 1000000) //1000到999999 千级
{
if (number % 1000 == 0)
{
return string.Format("{0} Thousand", ToEnglishString(number / 1000));
}
else
{
return string.Format("{0} Thousand and {1}", ToEnglishString(number / 1000), ToEnglishString(number % 1000));
}
}
if (number < 1000000000) //1000 000到999 999 999 百万级
{
if (number % 1000 == 0)
{
return string.Format("{0} Million", ToEnglishString(number / 1000000));
}
else
{
return string.Format("{0} Million and {1}", ToEnglishString(number / 1000000), ToEnglishString(number % 1000000));
}
}
if (number <= int.MaxValue) //十亿 级
{
if (number % 1000000000 == 0)
{
return string.Format("{0} Billion", ToEnglishString(number / 1000000000));
}
else
{
return string.Format("{0} Billion and {1}", ToEnglishString(number / 1000000000), ToEnglishString(number % 1000000000));
}
}
return "";
}
/// <summary>
/// 字符串简体转繁体
/// </summary>
/// <param name="strSimple"></param>
/// <returns></returns>
public static string ToTraditionalChinese(string strSimple)
{
if (string.IsNullOrEmpty(strSimple))
{
return strSimple;
}
return Strings.StrConv(strSimple, VbStrConv.TraditionalChinese, 0);
}
/// <summary>
/// 字符串繁体转简体
/// </summary>
/// <param name="strTraditional"></param>
/// <returns></returns>
public static string ToSimplifiedChinese(string strTraditional)
{
if (string.IsNullOrEmpty(strTraditional))
{
return strTraditional;
}
return Strings.StrConv(strTraditional, VbStrConv.SimplifiedChinese, 0);
}
public static string ToDateFormat(string val)
{
if (!string.IsNullOrEmpty(val))
{
DateTime date1 = DateTime.MinValue;
if (DateTime.TryParse(val, out date1))
{
return date1.ToString("yyyy-MM-dd");
}
}
return val;
}
public static string ToDateTimeFormat(string val)
{
if (!string.IsNullOrEmpty(val))
{
DateTime date1 = DateTime.MinValue;
if (DateTime.TryParse(val, out date1))
{
return date1.ToString("yyyy-MM-dd HH:mm:ss");
}
}
return val;
}
public static string SimpleEnglishToDateTimeFormat(DateTime SourceDate)
{
string Day = SourceDate.Day.ToString();
string Month = MonthConvertSimpleEnglish(SourceDate.Month.ToString());
string Year = SourceDate.Year.ToString();
List<string> DateTimeFormat = new List<string> { Day, Month, Year };
return string.Join("-", DateTimeFormat);
}
public static string MonthConvertSimpleEnglish(string SourceMonth)
{
string TargetMonth = string.Empty;
switch (SourceMonth)
{
case "1":
TargetMonth = "Jan";
break;
case "2":
TargetMonth = "Feb";
break;
case "3":
TargetMonth = "Mar";
break;
case "4":
TargetMonth = "Apr";
break;
case "5":
TargetMonth = "May";
break;
case "6":
TargetMonth = "Jun";
break;
case "7":
TargetMonth = "Jul";
break;
case "8":
TargetMonth = "Aug";
break;
case "9":
TargetMonth = "Sept";
break;
case "10":
TargetMonth = "Oct";
break;
case "11":
TargetMonth = "Nov";
break;
case "12":
TargetMonth = "Dec";
break;
}
return TargetMonth;
}
/// <summary>
/// 字符串替换 只替换第一次出现的字符
/// </summary>
/// <param name="SourceStr">源字符串</param>
/// <param name="OldValue">被替换字符</param>
/// <param name="NewValue">替换字符</param>
/// <returns></returns>
public static string ReplaceOne(string SourceStr, string OldValue, string NewValue)
{
int startIndex = SourceStr.IndexOf(OldValue);
int charLength = startIndex + OldValue.Length;
string ResultStr = SourceStr.Substring(0, charLength).Replace(OldValue, NewValue) + SourceStr.Substring(charLength);
return ResultStr;
}
}
}
C#String工具方法
猜你喜欢
转载自blog.csdn.net/qq_24432127/article/details/86700971
今日推荐
周排行