2011年12月26日 星期一

Visual Studio Code Auto Format

Select the text, hold CTRL and hit K followed by F (CTRL+K-F)
or (Ctrl + E -D) to format the whole document

2011年12月13日 星期二

半形轉全形

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine(new Program().ToWChar("abcd,.123"));
        }
        string ToWChar(string v)
        {
            Encoding big5 = Encoding.GetEncoding("big5");
            StringBuilder sb = new StringBuilder();
            int ascii = 0;
            foreach (char c in v.ToCharArray())
            {
                // Big5難字檢查
                string cInBig5 = big5.GetString(big5.GetBytes(new char[] { c }));
                ascii = Convert.ToInt32(c);
                if (ascii == 32 || (c != '?' && cInBig5 == "?")) // 難字亦使用全型空白取代 
                    sb.Append(Convert.ToChar(12288)); // 全型空白 
                else if (ascii < 65248) // 半形檢查 
                    sb.Append(Convert.ToChar(ascii + (ascii < 127 ? 65248 : 0))); // 半型轉全型 
                else
                    sb.Append(Convert.ToChar(ascii));
            }
            return sb.ToString();
        } 
    }
}