加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

数组 – 更改数组Delphi中的特殊字符

发布时间:2020-12-15 04:30:07 所属栏目:大数据 来源:网络整理
导读:我得到的一些字符串是UTF-8编码,并包含一些特殊字符,如 ??,?’,?等.我使用StringReplace()将其转换为一些普通文本,但我只能转换一种类型的字符.因为 PHP还有一个替换字符串的功能,如下所示: how to replace special characters with the ones they’re base
我得到的一些字符串是UTF-8编码,并包含一些特殊字符,如
??,?’,?等.我使用StringReplace()将其转换为一些普通文本,但我只能转换一种类型的字符.因为 PHP还有一个替换字符串的功能,如下所示: how to replace special characters with the ones they’re based on in 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不支持数组.

解决方法

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
);

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读