在GitHub上有个项目,本来是作为自己研究学习.net core的Demo,没想到很多同学在看,还给了很多星,所以觉得应该升成3.0,整理一下,写成博分享给学习.net core的同学们。
项目名称:Asp.NetCoreExperiment
项目地址:https://github.com/axzxs2001/Asp.NetCoreExperiment
今天先分享几个对日编程的小知识点
1、关于BOM(Byte Order Mark)知识点( https://baike.baidu.com/item/BOM/2790364)
Bom格式就是在开头添加三个字节-EF,BB,BF,来声明UTF-8格式
在.net core中,生成Bom的代码如下:
1 class Program 2 { 3 static void Main(string[] args) 4 { 5 GenerateBom1(); 6 GenerateBom2(); 7 } 8 /// <summary> 9 /// 方式一,利用UTF8Encoding构造函数来添加EF,BB,BF开头字节流 10 /// </summary> 11 static void GenerateBom1() 12 { 13 var encoding = new UTF8Encoding(true); 14 var filePath = Directory.GetCurrentDirectory() + "/bom1.txt"; 15 if (File.Exists(filePath)) 16 { 17 File.Delete(filePath); 18 } 19 using (var reader = new StreamWriter(filePath, false, encoding)) 20 { 21 //写入的内容 22 reader.WriteLine("bom1"); 23 reader.Close(); 24 } 25 } 26 /// <summary> 27 /// 方式二,手动方式添加EF,BB,BF开头字节流 28 /// </summary> 29 static void GenerateBom2() 30 { 31 //写入的内容 32 var list = new List<byte>(Encoding.UTF8.GetBytes("bom2")); 33 list.InsertRange(0, new byte[] { 239, 187, 191 });// EF=239 BB=187 BF=191 34 var filePath = Directory.GetCurrentDirectory() + "/bom2.txt"; 35 if (File.Exists(filePath)) 36 { 37 File.Delete(filePath); 38 } 39 using (var stream = new FileStream(filePath, FileMode.CreateNew)) 40 { 41 stream.Write(list.ToArray(), 0, list.Count); 42 stream.Close(); 43 } 44 } 45 }
2、日本日历
1 static void JapaneseTest() 2 { 3 var dt = DateTime.Now; 4 Console.WriteLine(dt); 5 //日本日历 6 var jpci = new System.Globalization.CultureInfo("ja-JP"); 7 var jpcal = new System.Globalization.JapaneseCalendar(); 8 jpci.DateTimeFormat.Calendar = jpcal; 9 Console.WriteLine(dt.ToString("ggyy/MM/dd", jpci)); //令和01/10/07 10 }
3、日语字符串转换Hankana,Katakana,Hiragana
1 class Program 2 { 3 private const string Hankana = 4 "ァアィイゥウェエォオカガキギクグケゲコゴサザシジスズセゼソゾタダチヂッツヅテデトドナニヌネノハバパヒビピフブプヘベペホボポマミムメモャヤュユョヨラリルレロワヲンヴヷヺ゙゚ー。「」、・"; 5 6 private const string Katakana = 7 "ァアィイゥウェエォオカガキギクグケゲコゴサザシジスズセゼソゾタダチヂッツヅテデトドナニヌネノハバパヒビピフブプヘベペホボポマミムメモャヤュユョヨラリルレロワヲンヴu30F7u30FA゛゜ー。「」、・"; 8 9 private const string Hiragana = 10 "ぁあぃいぅうぇえぉおかがきぎくぐけげこごさざしじすずせぜそぞただちぢっつづてでとどなにぬねのはばぱひびぴふぶぷへべぺほぼぽまみむめもゃやゅゆょよらりるれろわをんu3094u30F7u30FA" + 11 "゛゜ー。「」、・"; 12 13 private const string KatakanaOdoriji = "ヽヾ"; 14 15 private const string HiraganaOdoriji = "ゝゞ"; 16 17 static void Main(string[] args) 18 { 19 Console.OutputEncoding = Encoding.UTF8; 20 // Katakana-Hankana 21 Console.WriteLine($"========================KatakanaToHankana======================"); 22 Console.WriteLine($"old Katakana:{Katakana}"); 23 Console.WriteLine($"converted Hankana:{KanaConverter.Convert(Katakana, KanaOption.KatakanaToHankana)}"); 24 Console.WriteLine(); 25 26 //Hankana-Katakana 27 Console.WriteLine($"========================HankanaToKatakana======================="); 28 Console.WriteLine($"old Hankana:{Hankana}"); 29 Console.WriteLine($"converted Katakana:{KanaConverter.Convert(Hankana, KanaOption.HankanaToKatakana)}"); 30 Console.WriteLine(); 31 32 // Hiragana-Hankana 33 Console.WriteLine($"========================HiraganaToHankana======================="); 34 Console.WriteLine($"old Hiragana:{Hiragana}"); 35 Console.WriteLine($"converted Hankana:{KanaConverter.Convert(Hiragana, KanaOption.HiraganaToHankana)}"); 36 Console.WriteLine(); 37 38 //Hankana--Hiragana 39 Console.WriteLine($"========================HankanaToHiragana======================="); 40 Console.WriteLine($"old Hankana:{Hankana}"); 41 Console.WriteLine($"converted Hiragana:{KanaConverter.Convert(Hankana, KanaOption.HankanaToHiragana)}"); 42 Console.WriteLine(); 43 44 //Katakana-Hiragana 45 Console.WriteLine($"========================KatakanaToHiragana======================="); 46 Console.WriteLine($"old Katakana:{Katakana}"); 47 Console.WriteLine($"converted Hiragana:{KanaConverter.Convert(Katakana, KanaOption.KatakanaToHiragana)}"); 48 Console.WriteLine(); 49 50 //Hiragana-Katakana 51 Console.WriteLine($"========================HiraganaToKatakana======================="); 52 Console.WriteLine($"old Hiragana:{Hiragana}"); 53 Console.WriteLine($"converted Katakana:{KanaConverter.Convert(Hiragana, KanaOption.HiraganaToKatakana)}"); 54 Console.WriteLine(); 55 56 //Katakana-Hiragana 57 Console.WriteLine($"========================KatakanaToHiragana======================="); 58 Console.WriteLine($"old KatakanaOdoriji:{KatakanaOdoriji}"); 59 Console.WriteLine($"converted HiraganaOdoriji:{KanaConverter.Convert(KatakanaOdoriji, KanaOption.KatakanaToHiragana)}"); 60 Console.WriteLine(); 61 62 //Hiragana-Katakana 63 Console.WriteLine($"========================HiraganaToKatakana======================="); 64 Console.WriteLine($"old HiraganaOdoriji:{HiraganaOdoriji}"); 65 Console.WriteLine($"converted KatakanaOdoriji:{KanaConverter.Convert(HiraganaOdoriji, KanaOption.HiraganaToKatakana)}"); 66 Console.WriteLine(); 67 Console.ReadLine(); 68 69 70 } 71 }
4、Shift-JIS格式输出
1 static void Main(string[] args) 2 { 3 Encoding.RegisterProvider(CodePagesEncodingProvider.Instance); 4 var jis = Encoding.GetEncoding("shift_jis"); 5 File.WriteAllText(Directory.GetCurrentDirectory() + "/a.txt", "こんにちは", jis); 6 }