shwicho 发表于 2013-2-7 02:04:23

助记符解决长而复杂的商品名称 C#判断字符串

有些商品的名称比较长,放到数据库中又记不住他的条形码,这给超市等一些储备商品的仓库带来了一定的烦恼,对某个商品进行盘点,因为这个商品名称很长输入浪费很长的时间,就让助记符帮你解决这个难题。
助记符是什么呢?就是根据输入的中文字符获取拼音的首字母。有人要问了,我的商品名称要是含有字母或数字怎么办?解决方案是按正常字符输入,至转换汉字。如果用户输入的时因为操作的失误输入了非法字符 例如:"/" "."等,会输出null,页面会把文本框隐藏。我们就要检查自己输入的参数了。
 
说了那么多,究竟怎么样来实现 获取汉字拼音的首字母呢? 怎么把 "中文" 转化成 "ZW" 呢? 实现 代码如下:
using System;using System.Text;namespace wicho.User{    /// <summary>    /// 获取汉语拼音首字母类    /// CopyRight 2011 Wicho Chiu   /// </summary>    public class ConvertChinese    {      private ConvertChinese() { }      /// <summary>      /// 获取输入字符的首字母      /// </summary>      /// <param name="chinese">传递的字符串</param>      /// <example> "中文" 转换为 "ZW"</example>      /// <returns>获取的字符</returns>      public static String Convert(String chinese)      {            char[] buffer = new char;            for (int i = 0; i < chinese.Length; i++)            {                buffer = chinese;                int num = System.Convert.ToInt32(chinese);                if (chinese >= 'A' && chinese <= 'Z' || chinese >= '0' && chinese <= '9')                {                  //大写字符和数字正常输出                  buffer = chinese;                }                else if (chinese >= 'a' && chinese <= 'z')                {                  //把小写字符转换为大写字母                  buffer = System.Convert.ToChar(num - 32);                }                else if (num >= 0 && num <= 47 || num >= 58 && num <= 64 || num >= 91 && num <= 96 || num >= 123 && num <= 127)                {                  //非法字符null来代替,文本框会自动隐藏                  buffer = '\0';                }                else                {                  buffer = Convert(chinese);                }            }            return new String(buffer);      }      ///   <summary>         ///   获取一个汉字的拼首字母      ///   </summary>         ///   <paramname= "chinese "> Unicode格式的一个字符 </param>         ///   <returns>拼音的首字母</returns>         public static char Convert(Char chinese)      {            Encoding gb2312 = Encoding.GetEncoding("GB2312");            Encoding unicode = Encoding.Unicode;            byte[] unicodeBytes = unicode.GetBytes(new Char[] { chinese });            byte[] asciiBytes = Encoding.Convert(unicode, gb2312, unicodeBytes);            int n = (int)asciiBytes << 8;            n += (int)asciiBytes;            //获取汉字拼音的大写首字母            if (In(0xB0A1, 0xB0C4, n)) return 'A';            if (In(0XB0C5, 0XB2C0, n)) return 'B';            if (In(0xB2C1, 0xB4ED, n)) return 'C';            if (In(0xB4EE, 0xB6E9, n)) return 'D';            if (In(0xB6EA, 0xB7A1, n)) return 'E';            if (In(0xB7A2, 0xB8c0, n)) return 'F';            if (In(0xB8C1, 0xB9FD, n)) return 'G';            if (In(0xB9FE, 0xBBF6, n)) return 'H';            if (In(0xBBF7, 0xBFA5, n)) return 'J';            if (In(0xBFA6, 0xC0AB, n)) return 'K';            if (In(0xC0AC, 0xC2E7, n)) return 'L';            if (In(0xC2E8, 0xC4C2, n)) return 'M';            if (In(0xC4C3, 0xC5B5, n)) return 'N';            if (In(0xC5B6, 0xC5BD, n)) return 'O';            if (In(0xC5BE, 0xC6D9, n)) return 'P';            if (In(0xC6DA, 0xC8BA, n)) return 'Q';            if (In(0xC8BB, 0xC8F5, n)) return 'R';            if (In(0xC8F6, 0xCBF0, n)) return 'S';            if (In(0xCBFA, 0xCDD9, n)) return 'T';            if (In(0xCDDA, 0xCEF3, n)) return 'W';            if (In(0xCEF4, 0xD188, n)) return 'X';            if (In(0xD1B9, 0xD4D0, n)) return 'Y';            if (In(0xD4D1, 0xD7F9, n)) return 'Z';            return '\0';      }      private static bool In(int Low, int High, int Value)      {            return ((Value <= High) && (Value >= Low));      }    }}
 
效果图:
http://hi.csdn.net/attachment/201108/24/0_1314176259w7Dm.gif
页: [1]
查看完整版本: 助记符解决长而复杂的商品名称 C#判断字符串