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

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

这个问题有点模糊,不好回答,嘻嘻

你问题的重点是要有一个拥有存放有汉字首字母的列的数据库

只要你拥有了这个数据库,那么查询就不是问题,只要你输入首字母,在你编写的C#与数据库打交道的语句中,你用一个模糊查询方法,把输入的首字母(汉字的首字母HZ)设为查询条件,那么执行的时候C#就会从数据库中将满足条件的(如满足HZ)的全部数据从数据库中读取出来

我做过一个简单的KTV系统,其中拼音点歌就用到这个。

希望我的回答能够帮到你

这个可以参考一下:

{

// 简体中文的编码范围从B0A1(45217)一直到F7FE(63486)

private static int BEGIN = 45217

private static int END = 63486

// 按照声母表示,这个表是在GB2312中的出现的第一个汉字,也就是说“啊”是代表首字母a的第一个汉字。

// i, u, v都不做声母, 自定规则跟随前面的字母

private static char[] chartable = { '啊', '芭', '擦', '搭', '蛾', '发', '噶', '哈',

'哈', '击', '喀', '垃', '妈', '拿', '哦', '啪', '期', '然', '撒', '塌', '塌',

'塌', '挖', '昔', '压', '匝', }

// 二十六个字母区间对应二十七个端点

// GB2312码汉字区间十进制表示

private static int[] table = new int[27]

// 对应首字母区间表

private static char[] initialtable = { 'a', 'b', 'c', 'd', 'e', 'f', 'g',

'h', 'h', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',

't', 't', 'w', 'x', 'y', 'z', }

//得到首拼音码需先在InitializeComponent()函数后,得到GB2312码的首字母区间端点表

public static void Pymtable()

{

//为取得首拼音添加代码

for (int i = 0i <26i++)

{

table[i] = gbValue(chartable[i])// 得到GB2312码的首字母区间端点表,十进制。

}

table[26] = END// 区间表结尾

}

public static String GetPym(String SourceStr)

{

String Result = ""

int StrLength = SourceStr.Length

int i

try

{

for (i = 0i <StrLengthi++)

{

Result += Char2Initial(SourceStr[i])

}

}

catch (Exception ex)

{

MessageBox.Show(ex.ToString())

Result = ""

}

return Result

}

public static char Char2Initial(char ch)

{

// 对英文字母的处理:小写字母转换为大写,大写的直接返回

if (ch >= 'a' &&ch <= 'z')

return (char)(ch - 'a' + 'A')

if (ch >= 'A' &&ch <= 'Z')

return ch

// 对非英文字母的处理:转化为首字母,然后判断是否在码表范围内,

// 若不是,则直接返回。

// 若是,则在码表内的进行判断。

int gb = gbValue(ch)// 汉字转换首字母

if ((gb <BEGIN) || (gb >END))// 在码表区间之前,直接返回

return ch

int i

for (i = 0i <26i++)

{// 判断匹配码表区间,匹配到就break,判断区间形如“[,)”

if ((gb >= table[i]) &&(gb <table[i + 1]))

break

}

if (gb == END)

{//补上GB2312区间最右端

i = 25

}

return initialtable[i]// 在码表区间中,返回首字母

}

/*** 取出汉字的编码 cn 汉字*/

public static int gbValue(char ch)

{

// 将一个汉字(GB2312)转换为十进制表示。

string str = ""

str += ch

try

{

gb2312string df = new gb2312string()

df = str

byte[] bytes = df.ToByteArray()

if (bytes.Length <2)

return 0

return (bytes[0] <<8 &0xff00) + (bytes[1] &0xff)

}

catch (Exception e)

{

MessageBox.Show(e.ToString())

return 0

}

}

#region 是否存在生僻字

/// <summary>

/// 是否存在生僻字

/// </summary>

/// <param name="value"></param>

/// <returns></returns>

public static bool HasUnfamiliarWord(string value)

{

int start = 0xE000

int end = 0xAFFE

string s = ""

s = value

char[] cs = s.ToCharArray()

foreach (char item in cs)

{

int i = Convert.ToInt32(item)

if (i >= start &&i <= end)//unicode 在0xF8F0和0xAFFE之间的为生僻字

{

return true

}

}

return false

}

#endregion


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/sjk/9915046.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-03
下一篇 2023-05-03

发表评论

登录后才能评论

评论列表(0条)

保存