C# 字典(dictionary)练习,日期翻译
来源:互联网 发布:淘宝联盟如何购买省钱 编辑:程序博客网 时间:2024/06/10 20:54
编写一个函数进行日期转换,将输入的中文日期转换为阿拉伯数字日期,比如:二零一二年十二月二十一日要转换为2012-12-21。(处理“十”的问题:1.*月十日;2.*月十三日;3.*月二十三日;4.*月三十日;)4中情况对“十”的不同翻译。1→10;2→1;3→不翻译;4→0【年部分不可能出现’十’,都出现在了月与日部分。】
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace _13年月日转换{ class Program { static void Main(string[] args) { //案例:编写一个函数进行日期转换,将输入的中文日期转换为阿拉伯数字日期,比如:二零一二年十二月二十一日要转换为2012-12-21。(处理“十”的问题:1.*月十日;2.*月十三日;3.*月二十三日;4.*月三十日;)4中情况对“十”的不同翻译。1→10;2→1;3→不翻译;4→0【年部分不可能出现’十’,都出现在了月与日部分。】 //测试数据:二零一二年十二月二十一日(2012年12月21日)、二零零九年七月九日、二零一零年十月二十四日、二零一零年十月二十日 string str = "二零一零年十月二十日";//2012-12-21 string ziDian = "零0 一1 二2 三3 四4 五5 六6 七7 八8 九9"; //给集合添加数据 Dictionary<char, char> dic = new Dictionary<char, char>(); string[] temp = ziDian.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); for (int i = 0; i < temp.Length; i++) { dic.Add(temp[i][0], temp[i][1]); } string result = string.Empty; //翻译 for (int i = 0; i < str.Length; i++) { if (dic.ContainsKey(str[i])) { result += dic[str[i]]; } else { //不包含 十 年月日 //如果是年月日 if (str[i] == '十') { //1.*月十日;2.*月十三日;3.*月二十三日;4.*月三十日 if (!dic.ContainsKey(str[i - 1]) && !dic.ContainsKey(str[i + 1])) { result += "10"; } else if (!dic.ContainsKey(str[i - 1]) && dic.ContainsKey(str[i + 1])) { result += "1"; } else if (dic.ContainsKey(str[i - 1]) && dic.ContainsKey(str[i + 1])) { } else { result += "0"; } } else { //年月日 result += "-"; } } } result = result.TrimEnd('-'); Console.WriteLine(result); Console.ReadKey(); } }}
阅读全文
0 0
- C# 字典(dictionary)练习,日期翻译
- C# 字典(dictionary)练习,小写数字转大写
- C# Dictionary 字典
- C# Dictionary 字典
- C#的Dictionary字典
- C# 字典 Dictionary 遍历
- C# Dictionary 字典简介
- C# Dictionary 字典
- C# Dictionary 字典基础知识
- C# 字典 Dictionary 遍历
- C# Dictionary 字典
- C# 字典 dictionary
- C# Dictionary字典类介绍
- Python学习练习3----Dictionary(字典)
- C#中字典的使用Dictionary
- C#中字典的使用Dictionary
- C# Dictionary 泛型字典集合
- C#中的Dictionary字典类介绍
- redis学习三,Redis主从复制和哨兵模式
- bzoj2424 [HAOI2010]订货(费用流)
- mdk中在文件中加入工程标识
- python基础(02)——数据类型转化以及运算
- [leetcode] 47. Permutations II
- C# 字典(dictionary)练习,日期翻译
- IntelliJ IDEA 配合Maven使用Junit进行代码测试
- HTTP & HTTPS, Session & Cookie 知识点总结
- Java多线程控制
- JDBC笔记(二)SQL攻击
- DID YOU ?
- 最新的微信h5牛牛源码搭建教程分享
- Zookeeper的一致性协议:Zab
- 第7节项目3-买玫瑰问题