Autor Beitrag
n-regen
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 202
Erhaltene Danke: 2



BeitragVerfasst: Fr 03.10.08 10:25 
Hallo!

Da ich neben Delphi auch PHP programmiere, sind mir schon einige Funktionen von PHP aufgefallen, die es in Delphi nicht gibt. StrTr() ist eine davon.
Also habe ich die Funktion, die bestimmte Zeichen in einem String durch andere ersetzt, nachprogrammiert:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
function StrTr(Str:string; FromStr:string; ToStr:string;):string;
var i:integer;
begin
 Result := '';
 for i := 1 to length(Str) do begin
  if pos(Str[i], FromStr) <> 0
  then Result := Result + ToStr[pos(Str[i], FromStr)]
  else Result := Result + Str[i];
 end;
end;

Die Syntax entspricht der in PHP, davon abgesehen, dass mein StrTr nicht mit Arrays umgehen kann. Ihr könnt also alles erforderliche in der PHP-Manual nachlesen: de.php.net/manual/de/function.strtr.php

Ich hoffe wie immer, dass außer mir noch jemand die Funktion brauchen kann.
Gausi
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 8535
Erhaltene Danke: 473

Windows 7, Windows 10
D7 PE, Delphi XE3 Prof, Delphi 10.3 CE
BeitragVerfasst: Fr 03.10.08 10:33 
Deinen Aufwand in allen Ehren, aber es gibt da so eine Funktion StringReplace. Außerdem funktioniert deine Funktion nur mit Strings, die ein Zeichen lang sind, die beiden hinteren Parameter sollten als Char sein. ;-)

_________________
We are, we were and will not be.
jaenicke
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 19272
Erhaltene Danke: 1740

W11 x64 (Chrome, Edge)
Delphi 11 Pro, Oxygene, C# (VS 2022), JS/HTML, Java (NB), PHP, Lazarus
BeitragVerfasst: Fr 03.10.08 10:34 
StringReplace bzw. AnsiReplaceStr ist das eingebaute Pendant dazu in Delphi, letzteres in der Unit StrUtils.
GTA-Place
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
EE-Regisseur
Beiträge: 5248
Erhaltene Danke: 2

WIN XP, IE 7, FF 2.0
Delphi 7, Lazarus
BeitragVerfasst: Fr 03.10.08 10:57 
Seine Funktion macht aber IMHO leicht was anderes als StringReplace.

ausblenden Delphi-Quelltext
1:
StrTr('Drei kleine Chinesen''edc''atk');  // --> Trai klaina Khinasan					


Aus e wird a, aus d wird t und aus c wird k. In einem Rutsch. So hab ich das verstanden (aber nicht ausprobiert).

_________________
"Wer Ego-Shooter Killerspiele nennt, muss konsequenterweise jeden Horrorstreifen als Killerfilm bezeichnen." (Zeit.de)
Narses
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Administrator
Beiträge: 10181
Erhaltene Danke: 1254

W10ent
TP3 .. D7pro .. D10.2CE
BeitragVerfasst: Fr 03.10.08 11:08 
Moin!

user profile iconGTA-Place hat folgendes geschrieben Zum zitierten Posting springen:
Aus e wird a, aus d wird t und aus c wird k. In einem Rutsch. So hab ich das verstanden (aber nicht ausprobiert).
Sehe ich auch so. ;)

Allerdings ist das Konzept in diesem Fall nicht gut: deutlich besser wäre es, am Anfang eine Ersetzungstabelle aufzubauen und dann inline den Ergebnisstring zu ersetzen. :idea: So ist das ein Performance-Killer und eine Speicherschleuder... :?

cu
Narses

_________________
There are 10 types of people - those who understand binary and those who don´t.
Gausi
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 8535
Erhaltene Danke: 473

Windows 7, Windows 10
D7 PE, Delphi XE3 Prof, Delphi 10.3 CE
BeitragVerfasst: Fr 03.10.08 11:26 
Ich geb mir einfach mal selbst den Dieter Nuhr für heute. :oops:

_________________
We are, we were and will not be.
BenBE
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 8721
Erhaltene Danke: 191

Win95, Win98SE, Win2K, WinXP
D1S, D3S, D4S, D5E, D6E, D7E, D9PE, D10E, D12P, DXEP, L0.9\FPC2.0
BeitragVerfasst: Fr 03.10.08 13:14 
Also ich hab da mal eben aus'm Kopf was gescriptet ... Ungetestet, sollte aber:

ausblenden volle Höhe Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
function StrTr(Subject: String; FromStr, ToStr: String): String;
var
  TrTbl: Array [Char] of record
    c: Char;
    del: Boolean;
  end;
  C: Char;
  I: Integer;
  PS: PChar;
begin
  //Invalid Translation Table
  if Length(FromStr) < Length(ToStr) then
  Begin
    Result := Subject;
    exit;
  end;

  for C := Low(TrTbl) to High(TrTbl) do
  begin
    TrTbl[C].C := C;
    TrTbl[C].Del := False;
  end;
  for I := 1 to Length(FromStr) do
  begin
    If I < Length(ToStr) Then
    begin
      TrTbl[FromStr[C]].C = ToStr[I];
    end
    else
    begin
      TrTbl[FromStr[C]].Del = True;
    end;
  end;

  Result := Subject;
  PS := @Result[1];
  for I := 1 To Length(Subject) do
  begin
    With TrTbl[Subject[I]] do
    begin
      If Del Then
        Continue;
      PS^ := C;
      Inc(PS);
    end;
  end;
  SetLength(Result, (Integer(PS) - Integer(@Result[1])) div SizeOf(Char));
end;


Sollte auch mit den Unicode-Varianten von Delphi korrekt funktionieren ...

_________________
Anyone who is capable of being elected president should on no account be allowed to do the job.
Ich code EdgeMonkey - In dubio pro Setting.