更改数组Delphi中的特殊字符

发布于 2024-11-18 21:34:32 字数 933 浏览 6 评论 0原文

我得到的一些字符串是 UTF-8 编码的,并且包含一些特殊字符,例如 Å¡、ä'、ä 等。我正在使用 StringReplace() 将其转换为一些普通文本,但我只能转换一种类型的字符。因为 PHP 也有一个替换字符串的函数,如下所示: 如何用 PHP 中的特殊字符替换它们?,但它支持数组:

<?php
  $vOriginalString = "¿Dónde está el niño que vive aquí? En el témpano o en el iglú. ÁFRICA, MÉXICO, ÍNDICE, CANCIÓN y NÚMERO.";

  $vSomeSpecialChars = array("á", "é", "í", "ó", "ú", "Á", "É", "Í", "Ó", "Ú", "ñ", "Ñ");
  $vReplacementChars = array("a", "e", "i", "o", "u", "A", "E", "I", "O", "U", "n", "N");

  $vReplacedString = str_replace($vSomeSpecialChars, $vReplacementChars, $vOriginalString);

  echo $vReplacedString; // outputs '¿Donde esta el nino que vive aqui? En el tempano o en el iglu. AFRICA, MEXICO, INDICE, CANCION y NUMERO.'
?>

如何在 Delphi 中执行此操作? StringReplace 不支持数组。

Some string that I am getting is UTF-8 encoded, and contains some special characters like
Å¡, Ä‘, Ä etc. I am using StringReplace() to convert it to some normal text, but I can only convert one type of character. Because PHP also has a function to replace strings as seen here: how to replace special characters with the ones they're based on in PHP?, but it supports arrays:

<?php
  $vOriginalString = "¿Dónde está el niño que vive aquí? En el témpano o en el iglú. ÁFRICA, MÉXICO, ÍNDICE, CANCIÓN y NÚMERO.";

  $vSomeSpecialChars = array("á", "é", "í", "ó", "ú", "Á", "É", "Í", "Ó", "Ú", "ñ", "Ñ");
  $vReplacementChars = array("a", "e", "i", "o", "u", "A", "E", "I", "O", "U", "n", "N");

  $vReplacedString = str_replace($vSomeSpecialChars, $vReplacementChars, $vOriginalString);

  echo $vReplacedString; // outputs '¿Donde esta el nino que vive aqui? En el tempano o en el iglu. AFRICA, MEXICO, INDICE, CANCION y NUMERO.'
?>

How can I do this in Delphi? StringReplace doesn't support arrays.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

黒涩兲箜 2024-11-25 21:34:32
function str_replace(const oldChars, newChars: array of Char; const str: string): string;
var
  i: Integer;
begin
  Assert(Length(oldChars)=Length(newChars));
  Result := str;
  for i := 0 to high(oldChars) do
    Result := StringReplace(Result, oldChars[i], newChars[i], [rfReplaceAll])
end;

如果您担心由 StringReplace 引起的所有不必要的堆分配,那么您可以这样写:

function str_replace(const oldChars, newChars: array of Char; const str: string): string;
var
  i, j: Integer;
begin
  Assert(Length(oldChars)=Length(newChars));
  Result := str;
  for i := 1 to Length(Result) do
    for j := 0 to high(oldChars) do
      if Result[i]=oldChars[j] then
      begin
        Result[i] := newChars[j];
        break;
      end;
end;

像这样调用它:

newStr := str_replace(
  ['á','é','í'],
  ['a','e','i'], 
  oldStr
);
function str_replace(const oldChars, newChars: array of Char; const str: string): string;
var
  i: Integer;
begin
  Assert(Length(oldChars)=Length(newChars));
  Result := str;
  for i := 0 to high(oldChars) do
    Result := StringReplace(Result, oldChars[i], newChars[i], [rfReplaceAll])
end;

If you are concerned about all the needless heap allocations caused by StringReplace then you could write it this way:

function str_replace(const oldChars, newChars: array of Char; const str: string): string;
var
  i, j: Integer;
begin
  Assert(Length(oldChars)=Length(newChars));
  Result := str;
  for i := 1 to Length(Result) do
    for j := 0 to high(oldChars) do
      if Result[i]=oldChars[j] then
      begin
        Result[i] := newChars[j];
        break;
      end;
end;

Call it like this:

newStr := str_replace(
  ['á','é','í'],
  ['a','e','i'], 
  oldStr
);
苏佲洛 2024-11-25 21:34:32

消除口音称为标准化

由于您使用的是 Unicode,因此您不仅想规范化问题中的重音字符的简短列表。事实上,您正在寻找 Unicode 标准化形式 D (NFD) 或 KD (NFKD),您可以在 Windows 中执行此操作,当然也可以在 Delphi 中执行此操作。

这个答案应该得到你从理论上讲。

此 Delphi 代码这个答案应该让你去实施。

Getting rid of your accents is called Normalization.

Since you are using Unicode, you are not only wanting to normalize the short list of accented characters in your question. In fact you are looking for Unicode Normalization Form D (NFD) or KD (NFKD), which you can do in Windows and of course in Delphi.

This answer should get you going on the theoretical side.

This Delphi code and this answer should get you going implementing.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文