![]() |
Copy Pos Delete - Bin auf dem Holzweg...
Normalerweise ist dies einfach zu lösen, bin aber zulang
wach um noch klar denken zu können. Ich hab einen String, welcher folgendermaßen aussieht: Wort1,Wort2,Wort3,Wort4,Wort5 Ich will nun diese einzelnen Wörter in jeweils eine Edit packen. Sprich Wort1 in Edit1. Wort2 in Edit2 etc. Nur will das nicht hinhauen:
Delphi-Quellcode:
[Error] WZCreator.pas(105): Constant object cannot be passed as var parameter
procedure TForm1.Button2Click(Sender: TObject);
begin listbox1.items.LoadFromFile('File.1'); listbox2.items.LoadFromFile('File.2'); Memo2.text := (Listbox1.Items.Text) ; Memo3.text := (Listbox2.Items.Text) ; Memo1.Text := XorString(Memo2.Text, Memo3.Text); Edit1.text := Copy(memo1.text, 1, Pos(',', memo1.text) - 1); Delete(memo1.text, 1, Pos(',', memo1.text)); Edit2.text := Copy(memo1.text, 1, Pos(',', memo1.text) - 1); Delete(memo1.text, 1, Pos(',', memo1.text)); Edit3.text := Copy(memo1.text, 1, Pos(',', memo1.text) - 1); Delete(memo1.text, 1, Pos(',', memo1.text)); Edit4.text := Copy(memo1.text, 1, Pos(',', memo1.text) - 1); Delete(memo1.text, 1, Pos(',', memo1.text)); Edit5.text := Copy(memo1.text, 1, Pos(',', memo1.text) - 1); Delete(memo1.text, 1, Pos(',', memo1.text)); end; Was mach ich :wall: Habs schon:
Delphi-Quellcode:
Memo1.Text := XorString(Memo2.Text, Memo3.Text);
s := Memo1.Text; Edit1.text := Copy(s, 1, Pos(',', s) - 1); Delete(s, 1, Pos(',', s)); Edit2.text := Copy(s, 1, Pos(',', s) - 1); Delete(s, 1, Pos(',', s)); Edit3.text := Copy(s, 1, Pos(',', s) - 1); Delete(s, 1, Pos(',', s)); Edit4.text := Copy(s, 1, Pos(',', s) - 1); Delete(s, 1, Pos(',', s)); Edit5.text := Copy(s, 1, Pos(',', s) - 1); Delete(s, 1, Pos(',', s)); |
Re: Copy Pos Delete - Bin auf dem Holzweg...
copy braucht als zweiten parameter die POSITION da wo du überall die 1 stehen hast ;) und in dem 3. brauchst die zu kopierende länge ( nicht die position wie bei dir )
|
Re: Copy Pos Delete - Bin auf dem Holzweg...
Zitat:
|
Re: Copy Pos Delete - Bin auf dem Holzweg...
Wir hatten doch hier schon mal irgendwo einen Thread, in dem es um eine Funktion "Explode" ging (oder so ähnlich). Diese sollte einen String an einem bestimmten Zeichen "zerschneiden". Das könnte dir hier auch weiterhelfen... evtl. weiss die OH was dazu (ich weiss nicht mehr, ob es jetzt eine mit Delphi mitgelieferte Funktion war, oder ob die jemand von hier geschrieben hat...)
|
Re: Copy Pos Delete - Bin auf dem Holzweg...
ach jetzt wo du es sagst.. sehe ich das er auchnoch deleten o_O
Delphi-Quellcode:
function Tform1.splitOutIndex(Source: String; Delimiter: String; Index: Integer): String;
var count, number: Integer; giveback: String; begin count := 1; number := 0; giveback := ''; while (count <= length(Source)) and (number <= index) do begin if copy(Source, count, length(Delimiter)) = Delimiter then begin number := number + 1; count := count + length(Delimiter); end else begin if number = Index then giveback := giveback + Source[count]; count := count + 1; end; end; result := giveback; end; //-------------------------------------------------------- function Tform1.VBSplit(Liste: TStringList; Text2Split, SeperatorStr: String): Boolean; Var Posi : Longint; strTemp : String; strPart : String; bInLoop : Boolean; sepLen : Longint; begin result := true; bInLoop := false; try //Liste leeren Liste.clear; strTemp := Text2Split; sepLen := Length(SeperatorStr); Posi := Pos(SeperatorStr,strTemp); While Posi > 0 do begin bInLoop := true; strPart := Copy(strTemp,1,Posi-1); Liste.Add(strPart); strTemp := copy(strTemp,Posi+sepLen,Length(strTemp)-(Posi+sepLen-1)); Posi := Pos(SeperatorStr,strTemp); end; if (bInLoop) or (Length(strTemp)>0) then Liste.add(strTemp); except Result := false; end; end; //-------------------------------------------------------- |
Re: Copy Pos Delete - Bin auf dem Holzweg...
ola!
ich hab´s bei mir n bisschen kürzer:
Delphi-Quellcode:
hier wird der text in einem memo1 nach dem Trennzeichen "Plus" abgesucht, der Text davor erst kopiert, dann entsprechend abgeschnitten,und als separate Line in eine 2. Memobox geschrieben.
temp1 := memo1.Text;
repeat i := 0; repeat i := i + 1; until temp1[i] = '+'; memo2.Lines.Add(LeftStr(temp1,i-1)); Delete(temp1,1,i); until temp1 = ''; :) tyler |
Re: Copy Pos Delete - Bin auf dem Holzweg...
@Tyler: Die äußere Schleife würde ich zu einer While schleife umformen da es vorkommen kann das im Memo gar nix steht. Ansonsten wäre da nur noch anzumerken das sie ziemlich unperformant ist da ständig hinn und her kopiert wird etc. Ich hab hier mal so ne Function gepostet mit der Bitte das diese auf Performance hinn verbessert wird und rausgekommen ist eine procedure die recht schnell ist
Hier mal die Funktionen:
Delphi-Quellcode:
function splitOutIndex(const Source: String; const Delimiter: String; Index: Integer): String;
var Lcount, Lnumber, LSourceLength, LDelLength, LStart, LEnd: Integer; begin LCount := 1; Lnumber := 0; if Index = 0 then LStart := 1 else LStart := 0; LEnd := 0; result := ''; LSourceLength := length(Source); LDelLength := length(Delimiter); while (LCount <= LSourceLength) and (LEnd = 0) do begin if copy(Source, LCount, LDelLength) = Delimiter then begin inc(LNumber); if LNumber >= Index then begin if LStart = 0 then LStart := LCount + LDelLength else begin LEnd := LCount; result := copy(Source, LStart, LEnd - LStart); end; end; Lcount := Lcount + LDelLength; end else inc(LCount); end; if (LStart > 0) and (LEnd = 0) then result := copy(Source, LStart, LSourceLength - LStart + 1); end; procedure splitString(const Source: String; const Delimiter: String; var Dest: TStringlist); var count: Integer; LStartpos, LEndepos, LSourcelength: Integer; LDelimiterLength : Integer; begin Dest.Clear; count := 1; LStartpos := 0; LEndepos := 0; LSourcelength := length(Source); LDelimiterLength := Length(Delimiter); while count <= LSourcelength do begin if copy(Source, count, LDelimiterLength) = Delimiter then begin LEndepos := count; dest.Add(copy(Source, LStartpos + 1, LEndepos - LStartpos - 1)); LStartpos := count + LDelimiterLength - 1; inc(count,LDelimiterLength); end else begin inc(count); end; end; if LEndePos <> Count - LDelimiterLength then dest.Add(copy(Source, LStartpos + 1, count - LStartpos - 1)); end; |
Re: Copy Pos Delete - Bin auf dem Holzweg...
Delphi-Quellcode:
Schön kurz :wink:
function split(s:string; splitchar : char) : TStrings;
var a : integer; tmp : string; begin if NOT assigned(result) then exit; result := TStringlist.create; tmp := s; a := pos(splitchar,tmp); while a > 0 do begin delete(tmp,a,1); insert(#13#10,tmp,a); a := pos(splitchar,tmp); end; result.Text := tmp; end; |
Re: Copy Pos Delete - Bin auf dem Holzweg...
@Nightshade: Die function wird doch nie ausgeführt weil
Delphi-Quellcode:
immer dafür sorgt das die function frühzeitig verlassen wird. Und durch das Delete und Insert ziemlich unperformant
if NOT assigned(result) then exit;
|
Re: Copy Pos Delete - Bin auf dem Holzweg...
Zitat:
Also überspringt er sie immer..
Delphi-Quellcode:
Dann also so :)
function split(s:string; splitchar : char) : TStrings;
var a : integer; tmp : string; begin result := TStringlist.create; tmp := s; a := pos(splitchar,tmp); while a > 0 do begin delete(tmp,a,1); insert(#13#10,tmp,a); a := pos(splitchar,tmp); end; result.Text := tmp; end; |
Alle Zeitangaben in WEZ +1. Es ist jetzt 03:59 Uhr. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz