Autor Beitrag
mausi123
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 19



BeitragVerfasst: Mo 05.11.07 20:29 
Hallo, wie kann ich den folgenden code so ändern, um alle leerzeichen in memo1 zu löschen?

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
procedure TForm1.Button1Click(Sender: TObject);  
Var i,_pos:integer;  
    s:string;  
begin  
  for i:=0 to Memo1.Lines.count-1 do  
    begin  

      s:=memo1.lines[i];  
      _pos:=pos(' ',s);  
      if _pos<>0 then  
        //Leerzeichen gefunden, rest der zeile abknipsen  <-- genau andersrum solls sein
        begin  
          delete(s,_pos,length(s)-_pos+1);  
          memo1.lines[i]:=s;  
        end;  
    end;  
end;


Moderiert von user profile iconChristian S.: Delphi-Tags hinzugefügt
Moderiert von user profile iconChristian S.: Topic aus Dateizugriff verschoben am Mo 05.11.2007 um 19:41
Narses
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Administrator
Beiträge: 10183
Erhaltene Danke: 1256

W10ent
TP3 .. D7pro .. D10.2CE
BeitragVerfasst: Mo 05.11.07 20:55 
Moin und :welcome: im Forum!

user profile iconmausi123 hat folgendes geschrieben:
Hallo, wie kann ich den folgenden code so ändern, um alle leerzeichen in memo1 zu löschen?

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

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

Win XP, Win 7, BS2000
D5
BeitragVerfasst: Mo 05.11.07 20:57 
Hallo Mausi,

Dein Ansatz war schon ganz gut... nur ein paar kleine Änderungen müssen noch rein:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
Var
i,_pos:integer;
s:string;

begin
  for i:=0 to Memo1.Lines.count-1 do
   begin
      s:=memo1.lines[i];
      repeat                    // es könnten mehrere Leerzeichen in einer Zeile sein!?!
        _pos:=pos(' ',s);
        delete(s,_pos,1);       // Du willst ja nur 1 Zeichen (das Leerzeichen) löschen
        memo1.lines[i]:=s;
      until _pos=0;
   end;
end;


Damit werden alle Leerzeichen aus dem Memo gelöscht.

Viel Spaß

bflegel

//Edit: Wieder zu spät.

@Narses: Alter Spielverderber... so ists ja langweilig :wink:
Ich hab mich wenigstens an die Vorlage gehalten :D

_________________
I know all the jokes about my name
Narses
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Administrator
Beiträge: 10183
Erhaltene Danke: 1256

W10ent
TP3 .. D7pro .. D10.2CE
BeitragVerfasst: Mo 05.11.07 21:04 
Moin!

user profile iconbflegel hat folgendes geschrieben:
Ich hab mich wenigstens an die Vorlage gehalten :D
Dann würde ich´s aber auch so machen: ;)
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
  var
    i, _pos: Integer;
    s: String;
begin
  for i := 0 to Memo1.Lines.Count-1 do begin
    s := Memo1.Lines.Strings[i];
    _pos := Pos(' ',s);
    while (_pos > 0do begin
      Delete(s, _pos, 1);
      _pos := Pos(' ',s);
    end;
    Memo1.Lines.Strings[i] := s;
  end;
end;
cu
Narses

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

Win XP, Win 7, BS2000
D5
BeitragVerfasst: Mo 05.11.07 21:09 
Dann hast Du aber 2x die Zuweisung drinnen:

user profile iconNarses hat folgendes geschrieben:

_pos := Pos(' ',s);


Das erscheint mir nicht sinnvoll?!? Oder hat Deine Variante einen Vorteil, der mir bisher noch nicht klar wurde :?:

_________________
I know all the jokes about my name
Narses
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Administrator
Beiträge: 10183
Erhaltene Danke: 1256

W10ent
TP3 .. D7pro .. D10.2CE
BeitragVerfasst: Mo 05.11.07 21:10 
Moin!

user profile iconbflegel hat folgendes geschrieben:
Oder hat Deine Variante einen Vorteil, der mir bisher noch nicht klar wurde :?:
Was macht dein Code, wenn kein Leerzeichen im String ist? ;)

cu
Narses

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

Win XP, Win 7, BS2000
D5
BeitragVerfasst: Mo 05.11.07 21:13 
Gar nix!

Er rauscht einfach drüber. Das Programm stürzt mir nicht ab, er löscht keine Zeichen, die er nicht soll...

Also tut es genau das, wofür ich es geschrieben habe.

bye
bflegel

_________________
I know all the jokes about my name
Narses
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Administrator
Beiträge: 10183
Erhaltene Danke: 1256

W10ent
TP3 .. D7pro .. D10.2CE
BeitragVerfasst: Mo 05.11.07 21:18 
Moin!

user profile iconbflegel hat folgendes geschrieben:
Gar nix!
Er rauscht einfach drüber.
:? Hast Recht, Delete() ignoriert Aufrufe mit Index < 1. :|

user profile iconbflegel hat folgendes geschrieben:
Oder hat Deine Variante einen Vorteil, der mir bisher noch nicht klar wurde :?:
Er löscht nur, wenn auch was zum Löschen da ist... :lol: (ist also sog. "sauberer" Code; und um die Frage zu beantworten: nein ;))

cu
Narses

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

Win XP, Win 7, BS2000
D5
BeitragVerfasst: Mo 05.11.07 21:22 
Servus,

mit der Antwort kann ich leben. :wink:

Ich wünsche noch einen schönen Abend.
Schöne Grüße aus München.

Bye
bflegel

_________________
I know all the jokes about my name
Lannes
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2352
Erhaltene Danke: 4

Win XP, 95, 3.11, IE6
D3 Prof, D4 Standard, D2005 PE, TurboDelphi, Lazarus, D2010
BeitragVerfasst: Di 06.11.07 00:44 
Hallo,

user profile iconbflegel: Du solltest Deinen Code nochmal überdenken :wink:

Mit der Zeile:
memo1.lines[i]:=s;innerhalb der repeat-Schleife wird bei jedem gefundenen Leer-Zeichen die Zeile ins Memo geschrieben.

Hab Deinem Code mal einen Text mit 6000 Zeilen, in dem ca. 500.000 Leerzeichen enthalten sind, zur Bearbeitung übergeben.
Vermutlich wird er in ein paar Stunden fertig sein.

Schlage folgenden Code vor, er greift nur zweimal auf Memo.Text zu.:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
function DelSpace(s: String): String;
var z,i : Integer;
begin
  Setlength(Result,Length(s));
  i := 1;
  for z := 1 to Length(s) do
    if s[z] <> ' ' then
      begin
      Result[i] := s[z];
      inc(i);
      end;
      showmessage(IntToStr(i));
  Setlength(Result,i);
end;

//Aufruf:

Memo.Text := DelSpace(Memo.Text);

_________________
MfG Lannes
(Nichts ist nicht Nichts) and ('' <> nil ) and (Pointer('') = nil ) and (@('') <> nil )
bflegel
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 193
Erhaltene Danke: 1

Win XP, Win 7, BS2000
D5
BeitragVerfasst: Di 06.11.07 20:55 
Hallo Lannes,

6.000 Zeilen mit 500.000 Leerzeichen sind ein Argument. Ich habs natürlich nur mit 3-4 Zeilen getestet, in denen weniger als 10 Leerzeichen drin waren. Wenn man natürlich riesige Daten zu verarbeiten hat, ist meine Lösung wohl nicht so optimal :?

Bei den "Mengen", die ich normal verarbeite, fällt der Geschwindigkeitsunterschied gar nicht auf.

Danke für Deine Erläuterung.

Bye bflegel

_________________
I know all the jokes about my name