Autor Beitrag
Mentor86
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 46



BeitragVerfasst: Do 15.05.03 15:13 
Hi,
wie kann ich bestimmte Zeichen in einem Text durch andere ersetzen und dann ausgeben? So, das zum Beispiel aus A eine # wird.
Bitte helft.

MfG Mentor
gala
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 27



BeitragVerfasst: Do 15.05.03 15:25 
Titel: AnsiReplaceText (Funktion)
Ich glaube, du muss zuerst den ganzen Text in eine Variable einlesen, dann alle 'A' erzetzen und dann zurück in die Darei schreiben.

MfG

gala
Ex0rzist
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 550

Win XP Prof.
Mandrake 10.0

D6
BeitragVerfasst: Do 15.05.03 15:40 
Schau dir mal die Funktion StringReplace an (Online-Hilfe).

_________________
If accidentally read, induce vomitting.
Mentor86 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 46



BeitragVerfasst: Do 15.05.03 15:50 
Um ehrlich zu sein, habe ich das in der online Hilfe nicht verstanden. Wie würde denn die Programmzeile aussehen, in der z.B. A in B umgewandelt wird?

MfG Mentor
Ex0rzist
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 550

Win XP Prof.
Mandrake 10.0

D6
BeitragVerfasst: Do 15.05.03 16:04 
Zum Beispiel so:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
Procedure TForm1.Button1Click(Sender: TObject);
begin
 Memo1.Text := StringReplace(Memo1.Text,'A','B',[rfReplaceAll]);
end;

_________________
If accidentally read, induce vomitting.
gala
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 27



BeitragVerfasst: Do 15.05.03 16:05 
Titel: hier
AnsiReplaceStr(g,'A','B')

g ist dein Text

vergiss nicht,

uses StrUtils;

gala
Mentor86 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 46



BeitragVerfasst: Do 15.05.03 16:07 
Danke, es funktioniert.

MfG Dennis
Blackout
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 64



BeitragVerfasst: Do 15.05.03 16:12 
und wenn man keine extra Unit mit einbinden will:

ausblenden 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:
Function ReplaceString(Text, Old, New: String): String;

Var
  I: Integer;
  
Begin

  Repeat
    I:= Pos(Old, Text);
    If I > 0 Then
      Begin
        Delete(Text, I, Length(Old));
        Insert(New, Text, I);
        Text:= Text;
      End;
  Until I= 0;

  Result:= Text;
  
End;



Procedure TForm1.Button1Click(Sender: TObject);
Begin   

  Label1.Caption:= ReplaceString('H..A..L..L..O', '..', '');
 
End;

_________________
MfG Blackout
MathiasSimmack
Ehemaliges Mitglied
Erhaltene Danke: 1



BeitragVerfasst: Do 15.05.03 16:48 
Irgendwie habe ich ein Deja Vù, und ich möchte sakura zitieren:
Zitat:
Hallo du Endlosschleife.

@Blackout: Probier mal
ausblenden Quelltext
1:
Label1.Caption:= ReplaceString('HALLO','LL','LLL');					

und überlege mal wie du deine Funktion verbessern solltest.
Blackout
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 64



BeitragVerfasst: Do 15.05.03 20:16 
Das ist nicht meine Funktion :D

Aber stimmt jetzt bemerk ichs auch da dürfte was nicht funktionieren.
Edit: Ohne Repeat ja aber dann wirds ja nur einmal ersetzt und für solche war diese Funktion ja nicht ausgelegt (LL > LLL) :wink:

_________________
MfG Blackout
AndyB
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 1173
Erhaltene Danke: 14


RAD Studio XE2
BeitragVerfasst: Do 15.05.03 21:09 
Und warum sollte gleich StringReplace benutzt werden, wenn er doch nur ein Zeichen durch ein anderes Zeichen ersetzen will.

ausblenden Quelltext
1:
2:
3:
4:
5:
6:
7:
function CharReplace(const Text: string; Pattern: Char; Replace: Char): string;
var i: Integer;
begin
  Result := Text;
  for i := 1 to Length(Result) do
    if Result[i] = Pattern then Result[i] := Replace;
end;

_________________
Ist Zeit wirklich Geld?