c#怎么通过汉语拼音检索汉字?

2024-05-05 07:12

1. c#怎么通过汉语拼音检索汉字?

static public string GetChineseSpell(string strText)
		{
			int len = strText.Length;
			string myStr = "";
			for(int i=0;i<len;i++)
			{
				myStr += getSpell(strText.Substring(i,1));
			}
			return myStr;
		}

		static public string getSpell(string cnChar)
		{
			byte[] arrCN = Encoding.Default.GetBytes(cnChar);
			if(arrCN.Length > 1)
			{
				int area = (short)arrCN[0];
				int pos = (short)arrCN[1];
				int code = (area<<8) + pos;
				int[] areacode = {45217,45253,45761,46318,46826,47010,47297,47614,48119,48119,49062,49324,49896,50371,50614,50622,50906,51387,51446,52218,52698,52698,52698,52980,53689,54481};
				for(int i=0;i<26;i++)
				{
					int max = 55290;
					if(i != 25) max = areacode[i+1];
					if(areacode[i]<=code && code<max)
					{
						return Encoding.Default.GetString(new byte[]{(byte)(65+i)});
					}
				}
				return "*";
			}
			else return cnChar;
		}

调用方法例子:textBox3.Text=GetChineseSpell(textBox2.Text);

c#怎么通过汉语拼音检索汉字?

2. C#中如何用拼音模糊匹配汉字的首字母

最近在做一个模拟手机通讯录的管理器时,要用到拼音来查找联系人信息。id=15251下载安装包,下载完后解压vsintlpack1,里面有7个安装包,只需安装CHSPinYinConv(跟拼音相关)和CHTCHSConv(简体和繁体间的转换)这两个包就可以了,安装完毕后,需要在VS里添加引用,分别引用:C:\Program Files\Microsoft Visual Studio International Pack\Simplified Chinese Pin-Yin Conversion Library下和C:\Program Files\Microsoft Visual Studio International Pack\Traditional Chinese to Simplified Chinese Conversion Library and Add-In Tool下的dll即可使用(这两个路径是你安装时文件存放的位置,通过项目中的引用→浏览 转到安装文件的路径即可找到dll)。��四芴岣卟檠�俣龋�貌檠�氖�菰次�鸭釉厥�莸膁atagrid) using Microsoft.International.Converters.PinYinConverter; using System.Collections.ObjectModel; //为了方便,这里只引用关键命名空间////// 模糊查询 /////////privatevoid txtName_TextChanged(object sender, TextChangedEventArgs e) { List check = new List(); List list = new List(); list = (List)datagrid.Items.SourceCollection; if (string.IsNullOrEmpty(txtPin.Text)) //如果输入框为空时,加载全部数据 { BindDGD(); //重新绑定数据源 } elseif (txtPin.Text.Length>0) { //拼音模糊查询法for (int i = 0; i  roc = CC.Pinyins; //获取集合中第一个数据即为该汉字的拼音 chs=roc[0].ToLower().ToCharArray(); //将该汉字的拼音首字母追加到可变字符串中 str.Append(chs[0]); } } //判断该字符中是否包含输入的拼音if (str.ToString().Contains(txtPin.Text)) { check.Add(list[i]);//将查到的数据加到list中 } } datagrid.ItemsSource = check;//重新绑定数据 } } //绑定privatevoid BindDGD() { datagrid.ItemsSource = new T_PhoneContactBLL().ListAll(); }如果哪位有更好的方法,可给我留言,谢谢

3. C#中如何通过汉字拼音首字母检索筛选数据?

你这个必需是在录入的时候先把汉字的首字母也存入数据库,不然的话没办法实现,比如你打“HZ”,那杭州首字母也叫“HZ”,汉字也叫“HZ”,你可以在录入的时候让它自动转换,录入杭州自动生成一个“HZ”存入另一个字段,这个代码我这里有:

C#中如何通过汉字拼音首字母检索筛选数据?

4. c#通过汉字拼音字母进行数据库模糊查询

如果,将 汉字 与  字母拼音  一一对应。
这种数据库的设计很难的样子......——毕竟人为加入那么多的数据,不是简单的事情....

自己也冒得好的办法.....

5. 在C#中开发怎样按拼音查找到

建立 GB2312编码与拼音中首字母的对应关系。这个关系表可以帮助去查找某个汉字的读音。GB2312编程顺序是按拼音进行排序的。如果是UTF8,建立整体上UTF8到GB2312转换的函数,然后进行拼音查询(UTF8是按部首排序的)。
嗯,就是这么玩的!

在C#中开发怎样按拼音查找到

6. C#中有没有方法得到汉字的拼音

  /// 得到汉字的首字母        ///         /// 汉字(只能一个字)        /// 汉字字母        private static string GetGbkX(string str)        {            if (str.CompareTo("吖") < 0)            {                return str;            }            if (str.CompareTo("八") < 0)            {                return "A";            }            if (str.CompareTo("嚓") < 0)            {                return "B";            }            if (str.CompareTo("咑") < 0)            {                return "C";            }            if (str.CompareTo("妸") < 0)            {                return "D";            }            if (str.CompareTo("发") < 0)            {                return "E";            }            if (str.CompareTo("旮") < 0)            {                return "F";            }            if (str.CompareTo("铪") < 0)            {                return "G";            }            if (str.CompareTo("讥") < 0)            {                return "H";            }            if (str.CompareTo("咔") < 0)            {                return "J";            }            if (str.CompareTo("垃") < 0)            {                return "K";            }            if (str.CompareTo("呒") < 0)            {                return "L";            }            if (str.CompareTo("拏") < 0)            {                return "M";            }            if (str.CompareTo("噢") < 0)            {                return "N";            }            if (str.CompareTo("妑") < 0)            {                return "O";            }            if (str.CompareTo("七") < 0)            {                return "P";            }            if (str.CompareTo("亽") < 0)            {                return "Q";            }            if (str.CompareTo("仨") < 0)            {                return "R";            }            if (str.CompareTo("他") < 0)            {                return "S";            }            if (str.CompareTo("哇") < 0)            {                return "T";            }            if (str.CompareTo("夕") < 0)            {                return "W";            }            if (str.CompareTo("丫") < 0)            {                return "X";            }            if (str.CompareTo("帀") < 0)            {                return "Y";            }            if (str.CompareTo("咗") < 0)            {                return "Z";            }            return str;        }

7. C#判断指定字符串是否包含有汉字

/// 判断指定字符串是否包含有汉字
/// 指定的字符串
/// 若包含有汉字则返回True,否则返回False
public static bool InChinese1(string StrChineseString)
{

return System.Text.RegularExpressions. Regex.IsMatch(StrChineseString,@"[/u4e00-/u9fa5]+") ;
}

/// 判断指定字符串是否包含有汉字
/// 指定的字符串
/// 若包含有汉字则返回True,否则返回False
public static bool InChinese2(string StrChineseString)
{

int i = 0, m = 0;
m = StrChineseString.Length;
if (m < 1)
return false;
byte[] ucat = System.Text.Encoding.Default.GetBytes(StrChineseString);
for (i = 0; i <= ucat.Length - 1; i++)
{
m = ucat[i];
i += 1;
if (m > 160)
{
return true;
}
else
{
if (i < ucat.Length)
{
m = m*256 + ucat[i] - 65536;
if ((m > -20320) && (m < -10246))
return true;
}
}
}
return false;
}

C#判断指定字符串是否包含有汉字

8. C# 拼音转中文

首先说明一点,输入字母或拼音显示中文不是用C#实现的,而是用数据库实现的,数据库中有一列是中文,而还有一列是拼音的简写。我刚做过一个系统,用的是VS 2010 + SQL Server 2008实现的,其中一个功能就是拼音简写的模糊查询,我设计的思想就是在数据库中设计的有中文的和拼音简写的属性,当然拼音简写不是自己手工添加的,是在SQL SQL Server 2008建立触发器实现的。

--SQL中将汉字转换成拼音,这里是转换汉字首拼音,如果需要全部转换,在以下程序中稍微改几个数字就可以了

--SQL使用

--创建翻译函数

create function Fun_GetPY(@str nvarchar(4000))
returns nvarchar(4000)
as
begin
declare @word nchar(1),@PY nvarchar(4000)
set @PY=''
while len(@str)>0
begin
set @word=left(@str,1)
--如果非汉字字符,返回原字符
set @PY=@PY+(case when unicode(@word) between 19968 and 19968+20901
then (select top 1 PY from (
select 'A' as PY,N'骜' as word
union all select 'B',N'簿'
union all select 'C',N'错'
union all select 'D',N'鵽'
union all select 'E',N'樲'
union all select 'F',N'鳆'
union all select 'G',N'腂'
union all select 'H',N'夻'
union all select 'J',N'攈'
union all select 'K',N'穒'
union all select 'L',N'鱳'
union all select 'M',N'旀'
union all select 'N',N'桛'
union all select 'O',N'沤'
union all select 'P',N'曝'
union all select 'Q',N'囕'
union all select 'R',N'鶸'
union all select 'S',N'蜶'
union all select 'T',N'箨'
union all select 'W',N'鹜'
union all select 'X',N'鑂'
union all select 'Y',N'韵'
union all select 'Z',N'咗'
) T 
where word>=@word collate Chinese_PRC_CS_AS_KS_WS 
order by PY ASC) else @word end)
set @str=right(@str,len(@str)-1)
end
return @PY
end


--函数调用实例:
select dbo.Fun_GetPY('河南科技大学') --注意:一定要有dbo
--结果为:HNKJDX

--函数的删除
drop function dbo.Fun_GetPY


--实用案例

--表memberinfo中有字段(name,password,pinyin),拼音为ID的简写
--建立一个触发器,使表中每增加一行记录,pinyin中添加ID的简写
--注意:一张表中只能建立一个触发器

create trigger addpinyin 
on memberinfo for insert ,update
as 
declare @id varchar(10)
select @id=name from inserted
update memberinfo set pinyin=dbo.Fun_GetPY(@id)
where name IN (select name from inserted)


insert into memberinfo(ID,password)values('河南科技','471003')

select * from memberinfo
最新文章
热门文章
推荐阅读