string str = "1,2,3,4,5,6"; //如何把1替换成‘一年级’,2替换成‘二年级’....依次类推 //结果 “一年级,二年级,三年级,四年级,五年级,六年级”
求帮助??????????????????????????
public static string RemoveSpecialChar(this string str) { var dictionary = new Dictionary<char, string>() { {'1',"一年级"}, {'2','二年级'} }; var stringBuilder = new StringBuilder(str.Length); for (var i = 0; i < str.Length; i++) { var c = str[i]; if (dictionary.ContainsKey(c)) stringBuilder.Append(dictionary[c]); else stringBuilder.Append(c); } return stringBuilder.ToString(); }
类似这样,比多次使用Replace性能好些。
ok 谢谢了
static Dictionary<string, string> _numberCN = new Dictionary<string, string>() { { "1", "一" }, { "2", "二" }, { "3", "三" }, { "4", "四" }, { "5", "五" }, { "6", "六" }, { "7", "七" }, { "8", "八" }, { "9", "九" }, { "0", "零" } }; static void Main(string[] args) { string str = "1,2,3,4,5,6"; string[] _strArray = str.Split(','); StringBuilder _sb = new StringBuilder(); foreach (string _char in _strArray) { if (_numberCN.ContainsKey(_char)) { if (_sb.Length > 0) { _sb.Append(","); } _sb.Append(_numberCN[_char] + "年级"); } else { } } Console.WriteLine(_sb); Console.ReadLine(); }