delphi 字符串 转换 全角 半角
1. 编写一个函数,将字符串中的全角字符转换为半角字符。 2. 编写一个函数,将字符串中的半角字符转换为全角字符。 3. 测试这两个函数。 ### 代码 #### 1. 全角转半角函数 function ToHalfWidth(const S: string): string; var I: Integer; C: Char; begin Result := ''; for I := 1 to Length(S) do begin C := S[I]; if (Ord(C) >= $FF01) and (Ord(C) <= $FF5E) then Result := Result + Chr(Ord(C) - $FEE0) else if Ord(C) = $3000 then Result := Result + Chr($0020) else Result := Result + C; end; end; #### 2. 半角转全角函数 function ToFullWidth(const S: string): string; var I: Integer; C: Char; begin Result := ''; for I := 1 to Length(S) do begin C := S[I]; if (Ord(C) >= $0021) and (Ord(C) <= $007E) then Result := Result + Chr(Ord(C) + $FEE0) else if Ord(C) = $0020 then Result := Result + Chr($3000) else Result := Result + C; end; end; ### 运行代码 在 Delphi IDE 中创建一个新的控制台应用程序,将上述代码粘贴到项目的主文件中,然后运行程序。你将看到全角和半角字符串的转换结果。
Copyright © 2014 DelphiW.com 开发 源码 文档 技巧 All Rights Reserved
晋ICP备14006235号-8 晋公网安备 14108102000087号
执行时间: 0.045707225799561 seconds