oracle中fn_getpy函数,复制代码 代码如下: --函数 CREATE function fn_GetPy(@str nvarchar(4000)) returns nvarchar(4...

--函数

CREATE function fn_GetPy(@str nvarchar(4000))

returns nvarchar(4000)

--WITH ENCRYPTION

as

begin

declare @intLenint

declare @strRetnvarchar(4000)

declare @temp nvarchar(100)

set @intLen = len(@str)

set @strRet = ''

while @intLen > 0

begin

set @temp = ''

select @temp = case

when substring(@str,@intLen,1) >= '帀' then 'Z'

when substring(@str,@intLen,1) >= '丫' then 'Y'

when substring(@str,@intLen,1) >= '夕' then 'X'

when substring(@str,@intLen,1) >= '屲' then 'W'

when substring(@str,@intLen,1) >= '他' then 'T'

when substring(@str,@intLen,1) >= '仨' then 'S'

when substring(@str,@intLen,1) >= '呥' then 'R'

when substring(@str,@intLen,1) >= '七' then 'Q'

when substring(@str,@intLen,1) >= '妑' then 'P'

when substring(@str,@intLen,1) >= '噢' then 'O'

when substring(@str,@intLen,1) >= '拏' then 'N'

when substring(@str,@intLen,1) >= '嘸' then 'M'

when substring(@str,@intLen,1) >= '垃' then 'L'

when substring(@str,@intLen,1) >= '咔' then 'K'

when substring(@str,@intLen,1) >= '丌' then 'J'

when substring(@str,@intLen,1) >= '铪' then 'H'

when substring(@str,@intLen,1) >= '旮' then 'G'

when substring(@str,@intLen,1) >= '发' then 'F'

when substring(@str,@intLen,1) >= '妸' then 'E'

when substring(@str,@intLen,1) >= '咑' then 'D'

when substring(@str,@intLen,1) >= '嚓' then 'C'

when substring(@str,@intLen,1) >= '八' then 'B'

when substring(@str,@intLen,1) >= '吖' then 'A'

else rtrim(ltrim(substring(@str,@intLen,1)))

end

--对于汉字特殊字符,不生成拼音码

if (ascii(@temp)>127) set @temp = ''

--对于英文中小括号,不生成拼音码

if @temp = '(' or @temp = ')' set @temp = ''

select @strRet = @temp + @strRet

set @intLen = @intLen - 1

end

return lower(@strRet)

end

go

--调用

select dbo.fn_getpy('张三')

--返回:zs

答!: 2:

取汉字拼音首字母的存储过程

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: getpy()函数是一种将汉字转化为全拼拼音的代码函数。在使用该函数之前,需要先定义一个汉字字符集字典,以便将汉字转化为拼音。在该字典,每个汉字对应一个拼音字符串列表,其每个字符串都是该汉字的一个拼音。 具体代码实现是,首先将输入的汉字字符串逐个字符分离出来,并在定义的汉字字符集字典查找该字符对应的拼音列表。对于每个汉字字符,将其对应的拼音字符串列表的第一个拼音添加到一个列表,直到遍历完整个字符串。最后,将列表所有拼音字符串按照汉字顺序合并起来,就得到了输入字符串的全拼拼音字符串。 需要注意的是,在汉字字符集字典,一些汉字可能存在多音字情况,即一个汉字对应多个拼音字符串。在这种情况下,根据需要选择一个默认的拼音字符串或者提供选项让用户进行选择。此外,汉字还存在一些生僻字或繁体字,这些字符在字典可能不存在或者没有完整的拼音列表,需要进行特殊处理。 总之,getpy()函数实现了将汉字转化为全拼拼音的过程,为汉字在计算机程序的使用提供了方便和便捷。 ### 回答2: getpy函数的作用是将输入的文字符串转化为对应的全拼拼音字符串。下面是getpy函数代码实现: ```python import pypinyin def getpy(input_str): """ 将文转化为拼音 """ # 将字符串转化为列表 input_list = list(input_str) # 使用pypinyin库将文转化为拼音 pinyin_list = pypinyin.lazy_pinyin(input_list) # 使用join函数将拼音列表合并为字符串 py_str = ''.join(pinyin_list) return py_str ``` 该函数使用了pypinyin库来实现文到拼音的转换。在函数,首先将输入的文字符串转化为一个列表,然后使用pypinyin的lazy_pinyin函数将每个汉字转化为对应的拼音,得到一个拼音列表。最后,使用join函数将拼音列表合并为一个字符串并返回。 例如,当输入文字符串“你好世界”时,函数会返回“nihao shijie”的拼音字符串。需要注意的是,该函数只能将汉字转化为拼音,对于字符串的其他字符,函数会原样保留。 ### 回答3: getpy函数是一个将文字符串转换成全拼字符串的函数。以下是getpy函数的示例代码: ```python # -*- coding: utf-8 -*- import re def getpy(word): """ 将文字符串转成全拼字符串 """ pylist = [] for s in word: if '\u4e00' <= s <= '\u9fff': # 如果s是文字符,则使用re模块进行匹配 charcode = ord(s) - 0x4e00 # 文字符的编码范围为0x4e00-0x9fff,因此减去0x4e00即可得到对应数字 try: py = re.findall(pylist[charcode]['py'], pycode) # 在pylist查找对应拼音 pylist[charcode]['used'] = True # 标记已使用 except: py = [s] # 如果未找到对应拼音,则使用文字符本身 pylist.append({'py': py, 'used': False}) # 将拼音加入拼音列表 else: # 如果s不是文字符,则无需匹配,直接加入拼音列表 pylist.append({'py': [s], 'used': True}) # 按顺序输出拼音 pystring = '' for p in pylist: if not p['used']: pystring += ''.join(p['py']) return pystring ``` 该函数的实现原理是:首先判断输入的字符s是否是文字符,如果是,则在pylist查找文字符对应的拼音。如果找到,则加入拼音列表,并标记已使用;否则,将文字符本身加入拼音列表。最后,将拼音按顺序输出即可。 需要注意的是,该函数依赖于拼音库,因此在使用前需要将拼音库复制代码所在的目录下,并将其命名为“pinyin.txt”。拼音库的格式是每行一个文字符和它对应的拼音,以空格隔开。例如: ``` zhong1 文 wen2 字 zi4 符 fu2 ``` 该函数可以实现将汉字转换为对应的全拼字符串,并可以处理汉字和非汉字混合的情况。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值