程序代码为:

THzSpell.PyOfHz(Edit1.Text)//获取汉字的拼音

UpperCase(THzSpell.PyHeadOfHz(Edit1.Text))//获取拼音首字母

----------------------------------代码文件---------------------------------------------------------------------

unit HzSpell;
{ version 4.1}

interface

uses
  Windows, Messages, SysUtils, Classes;

type
  THzSpell = class(TComponent)
  protected
    FHzText: String;
    FSpell: String;
    FSpellH: String;
    procedure SetHzText(const Value: String);
    function GetHzSpell: String;
    function GetPyHead: String;
  public
    class function PyOfHz(Hz: String): String;
    class function PyHeadOfHz(Hz: String): String;
  published
    property HzText: String read FHzText write SetHzText;
    property HzSpell: String read GetHzSpell;
    property PyHead: String read GetPyHead;
  end;

{$I HzSpDat2.inc}

procedure Register;

function GetHzPy(HzChar: PChar; Len: Integer): String;
function GetHzPyFull(HzChar: String): String;
function GetHzPyHead(HzChar: PChar; Len: Integer): String;
function GetPyChars(HzChar: String): String;

implementation

procedure Register;
begin
  RegisterComponents('System', [THzSpell]);
end;
function GetHzPy(HzChar: PChar; Len: Integer): String;
var
  C: Char;
  Index: Integer;
begin
  Result := '';
  if (Len > 1) and (HzChar[0] >= #129) and (HzChar[1] >= #64) then
  begin
    //是否为 GBK 字符
    case HzChar[0] of
      #163:  // 全角 ASCII
      begin
        C := Chr(Ord(HzChar[1]) - 128);
        if C in ['a'..'z', 'A'..'Z', '0'..'9', '(', ')', '[', ']'] then
          Result := C
        else
          Result := '';
      end;
      #162: // 罗马数字
      begin
        if HzChar[1] > #160 then
          Result := CharIndex[Ord(HzChar[1]) - 160]
        else
          Result := '';
      end;
      #166: // 希腊字母
      begin
        if HzChar[1] in [#$A1..#$B8] then
          Result := CharIndex2[Ord(HzChar[1]) - $A0]
        else if HzChar[1] in [#$C1..#$D8] then
          Result := CharIndex2[Ord(HzChar[1]) - $C0]
        else
          Result := '';
      end;
      else
      begin  // 获得拼音索引
        Index := PyCodeIndex[Ord(HzChar[0]) - 128, Ord(HzChar[1]) - 63];
        if Index = 0 then
          Result := ''
        else
          Result := PyMusicCode[Index];
      end;
    end;
  end
  else if Len > 0 then
  begin
    //在 GBK 字符集外, 即半角字符
    if HzChar[0] in ['a'..'z', 'A'..'Z', '0'..'9', '(', ')', '[', ']',
      '.', '!', '@', '#', '$', '%', '^', '&', '*', '-', '+',
      '<', '>', '?', ':', '"'] then
      Result := HzChar[0]
    else
      Result := '';
  end;
end;
function GetHzPyFull(HzChar: String): String;
var
  i, len: Integer;
  Py: String;
  function IsDouByte(C: Char): Boolean;
  begin
    Result := C >= #129;
  end;
begin
  Result := '';
  i := 1;
  while i <= Length(HzChar) do
  begin
    if IsDouByte(HzChar[i]) and (Length(HzChar) - i > 0) then
      len := 2
    else
      len := 1;
    Py := GetHzPy(@HzChar[i], len);
    Inc(i, len);
    if (Result <> '') and (Py <> '') then
      Result := Result + ' ' + Py
    else
      Result := Result + Py;
  end;
end;
function GetHzPyHead(HzChar: PChar; Len: Integer): String;
begin
  Result := Copy(GetHzPy(HzChar, Len), 1, 1);
end;

function GetPyChars(HzChar: String): String;
var
  i, len: Integer;
  Py: String;
  function IsDouByte(C: Char): Boolean;
  begin
    Result := C >= #129;
  end;
begin
  Result := '';
  i := 1;
  while i <= Length(HzChar) do
  begin
    if IsDouByte(HzChar[i]) and (Length(HzChar) - i > 0) then
      len := 2
    else
      len := 1;
    Py := GetHzPyHead(@HzChar[i], len);
    Inc(i, len);
    Result := Result + Py;
  end;
end;
{ THzSpell }
function THzSpell.GetHzSpell: String;
begin
  if FSpell = '' then
  begin
    Result := GetHzPyFull(FHzText);
    FSpell := Result;
  end
  else Result := FSpell;
end;
function THzSpell.GetPyHead: String;
begin
  if FSpellH = '' then
  begin
    Result := GetPyChars(FHzText);
    FSpellH := Result;
  end
  else Result := FSpellH;
end;
class function THzSpell.PyHeadOfHz(Hz: String): String;
begin
  Result := GetPyChars(Hz);
end;
class function THzSpell.PyOfHz(Hz: String): String;
begin
  Result := GetHzPyFull(Hz);
end;
procedure THzSpell.SetHzText(const Value: String);
begin
  FHzText := Value;
  FSpell := '';
  FSpellH := '';
end;
end.

需要更多交流,请关注:http://weibo.com/u/2985316267?is_hot=1

Delphi获取汉字拼音以及拼音首字母相关推荐

  1. 获取汉字快拼(首字母),英文直接返回大写字母

    public static class PYMHelper{/// <summary>/// 获取字符串的首拼字母/// </summary>/// <param nam ...

  2. asp.net 获取汉字字符串的拼音首字母,含多音字

    需求:在很多时候数据查询的时候,我们希望输入某个人姓名的拼音首字母进行查询,例如"潘长江",输入"pcj",就能搜索潘长江相关信息. 实现: #region 获 ...

  3. C#汉字转拼音(npinyin)将中文转换成拼音全文或首字母

    汉字转拼音貌似一直是C#开发的一个难题,无论什么方案都有一定的bug,之前使用了两种方案. 1.Chinese2Spell.cs 一些不能识别的汉字全部转为Z 2.Microsoft Visual S ...

  4. Java获取中文拼音、中文首字母缩写和中文首字母

    我们有时候会遇到这样的情况,需要获取某些中文的拼音.中文首字母缩写和中文首字母,下面我将为大家介绍一下如何获取中文拼音的缩写. 1.项目建立和配置 首先,我们建立一个java项目,新建libs文件夹并 ...

  5. java汉字转拼音以及得到首字母通用方法

    首先需要引入一个jar包:pinyin4j-2.5.0.jar  链接: https://pan.baidu.com/s/1BJL7KvuJ7AXhSKQUloEi_g 提取码: v39m packa ...

  6. Excel汉字转拼音并提取首字母

    Excel汉字转拼音并提取首字母步奏 1.打开VB窗口alt+f11可以快捷打开 2.如下图新建模块 3.模块建好后插入代码 Function getpychar(char)tmp = 65536 + ...

  7. Java-汉字字符串转拼音,包括首字母和全拼

    本方法通过pinyin4j这个工具包来实现. 主要流程为先将字符串转换为char[]数组,再循环判断字符是否是汉字,这一步主要使用汉字Unicode编码进行匹配,是汉字则调用pinyin4j中的汉字转 ...

  8. 中英文混合拼音排序(从首字母开始,相同则往下比较)

    中英文混合拼音排序(从首字母开始,相同则往下比较) 中文/英文混合数据 进行转拼音(中文),按照26个字母顺序排序,从首字母开始比较,相同则比较第二个字母,如此类推,直到结束排序为止. pom.xml ...

  9. 获取汉字串的拼音助记码

    CREATE OR REPLACE FUNCTION FUN_SPELLCODE(PRM_SPELL IN VARCHAR2) RETURN VARCHAR2[@more@] /*---------- ...

  10. Excel中文转拼音(中文首字母)

    Excel中文转拼音(中文首字母) 1.打开Excel->Alt+F11->Viaual Basic编辑器 2.在弹出来的窗口中点击插入->模块->ctrl+s 保存 Func ...

最新文章

  1. 马斯克受罚离任董事长,默多克之子或将成接替者!
  2. [置顶]       安全-用户身份验证
  3. oracle-rman-list命令收集
  4. NSNotification消息通知实现源码(观察者模式原理)
  5. 图形处理(五)基于旋转不变量的网格变形-Siggraph 2007
  6. 设置模态框开始是隐藏状态_有限元仿真分析误差来源之材料参数设置,小心为妙!...
  7. HBase Shell基本操作
  8. LGB 的 .feature_importance() 函数
  9. CNS级lncRNA权威文章解读及前沿发展
  10. Spring AbstractBeanFactory
  11. tensorflow linux多卡训练_用Windows电脑训练深度学习模型?超详细配置教程来了
  12. pandas所占内存释放
  13. 部署Spring Boot Angular App(Maven和Tomcat)的4种方法
  14. 4个万无一失的技巧让您开始使用JBoss BRMS 6.0.3
  15. linux 高可用----keepalived+lvs
  16. 网页中的meta property
  17. 2018 CodeM复赛:B. 软件包管理器(二分)
  18. 阿里云跨云迁移工具案例实践:华为云迁移到阿里云
  19. Secondary NameNode工作机制
  20. 卡内基梅隆大学计算机专业录取难,卡内基梅隆大学申请难度分析

热门文章

  1. 微信小程序-JAVA实现微信支付功能(微信支付2.0)
  2. [从头读历史] 第271节 诗经 郑风
  3. Java面试题最新更新
  4. python文件夹自动备份_python实现定时自动备份文件到其他主机的实例代码
  5. 基于5G和MEC的新型智慧工地应用研究
  6. 人肉翻墙之生活在别处
  7. 系统网络“人肉”监控
  8. python数字转换为大写中文_python 人民币数字转汉字大写金额
  9. c#字典dic的用法总结
  10. 【新】Notion基础教程(持续更新中……)