Autor |
Beitrag |
n-regen
Beiträge: 202
Erhaltene Danke: 2
|
Verfasst: 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:
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
Beiträge: 8538
Erhaltene Danke: 475
Windows 7, Windows 10
D7 PE, Delphi XE3 Prof, Delphi 10.3 CE
|
Verfasst: 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
Beiträge: 19284
Erhaltene Danke: 1742
W11 x64 (Chrome, Edge)
Delphi 11 Pro, Oxygene, C# (VS 2022), JS/HTML, Java (NB), PHP, Lazarus
|
Verfasst: Fr 03.10.08 10:34
StringReplace bzw. AnsiReplaceStr ist das eingebaute Pendant dazu in Delphi, letzteres in der Unit StrUtils.
|
|
GTA-Place
Beiträge: 5248
Erhaltene Danke: 2
WIN XP, IE 7, FF 2.0
Delphi 7, Lazarus
|
Verfasst: Fr 03.10.08 10:57
Seine Funktion macht aber IMHO leicht was anderes als StringReplace.
Delphi-Quelltext 1:
| StrTr('Drei kleine Chinesen', 'edc', 'atk'); |
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
Beiträge: 10182
Erhaltene Danke: 1255
W10ent
TP3 .. D7pro .. D10.2CE
|
Verfasst: Fr 03.10.08 11:08
Moin!
GTA-Place hat folgendes geschrieben : | 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. 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
Beiträge: 8538
Erhaltene Danke: 475
Windows 7, Windows 10
D7 PE, Delphi XE3 Prof, Delphi 10.3 CE
|
Verfasst: Fr 03.10.08 11:26
Ich geb mir einfach mal selbst den Dieter Nuhr für heute.
_________________ We are, we were and will not be.
|
|
BenBE
Beiträge: 8721
Erhaltene Danke: 191
Win95, Win98SE, Win2K, WinXP
D1S, D3S, D4S, D5E, D6E, D7E, D9PE, D10E, D12P, DXEP, L0.9\FPC2.0
|
Verfasst: Fr 03.10.08 13:14
Also ich hab da mal eben aus'm Kopf was gescriptet ... Ungetestet, sollte aber:
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 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.
|
|
|