Autor Beitrag
Marco D.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 2750

Windows Vista
Delphi 7, Delphi 2005 PE, PHP 4 + 5 (Notepad++), Java (Eclipse), XML, XML Schema, ABAP, ABAP OO
BeitragVerfasst: Mi 12.07.06 20:05 
Ich habe mir folgende Prozedur zum Vertauschen zweier Substrings in einem String geschrieben, da ich keine vorgefertigte dafür gefunden habe. Gibt es eine?
Naja hier der Code:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
procedure TForm1.SwapSubStrings(var s : string; index1, index2, count : integer);
var temp:string;
begin
  if (index1+count-1<=index2) and (index2<Length(s)) then
  begin
    temp:=Copy(s,index1,count);
    Delete(s,index1,count);
    Insert(Copy(s,index2-count,count),s,index1);
    Delete(s,index2,count);
    Insert(temp,s,index2);
  end;
end;

Die Parameter sind selbsterklärend oder muss man die erläutern?
Inwieweit kann man diese optimieren? Arbeitet sie fehlerhaft? Bei mir hat es funktioniert (aus 'blubb' macht sie 'bbubbl' und aus '12345678' macht sie '56781234').
Bitte um Feedback :-D

_________________
Pascal keeps your hand tied. C gives you enough rope to hang yourself. C++ gives you enough rope to shoot yourself in the foot


Zuletzt bearbeitet von Marco D. am Fr 21.07.06 22:28, insgesamt 1-mal bearbeitet
fidionael
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 232

Win XP SP2, Ubuntu 6.06
Delphi 7 PE, Delphi 3 Prof
BeitragVerfasst: Do 13.07.06 01:11 
Hallo, soll gar nicht böse gemeint sein, aber ich habe jetzt schon ein wenig gegrübelt und mir ist beim besten Willen keine praktische Anwendung für deine Funktion eingefallen, hast du eine?

Und bei deinen Erläuterungen, was der Output eines versandten Strings ist solltest du vielleicht hinzufügen, welche Zahlen du für die weiteren Parameter eingesetzt hast; dann lässt sich dein Quelltext viel einfacher verstehen.

Mfg
crowley
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 406

Win XP, Win Vista, Mandriva, Ubuntu
Delphi 4-8, Delphi 2006, Delphi 2007
BeitragVerfasst: Do 13.07.06 07:42 
eine sinnvolle erweiterung wäre noch eine möglichkeit, unterschiedlich lange substr zu vertauschen... so dass du einen count für beide parameter übergibst..

eine überladene variante sollte vielleicht statt indizes die zu tauschenden substr als parameter bekommen...
Marco D. Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 2750

Windows Vista
Delphi 7, Delphi 2005 PE, PHP 4 + 5 (Notepad++), Java (Eclipse), XML, XML Schema, ABAP, ABAP OO
BeitragVerfasst: Do 13.07.06 14:08 
Also ich muss die Delphi-Farbcodes in Hexadezimal-Zahlen umwandeln und dafür Zeichen austauschen.
Ok, nehmen wir mal als Input 'blubb'. Dann ruft man die Procedure so auf:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
var s:string;
s:='blubb';
SwapString(s,1,4,2);
showmessage(s);

Das Ergebnis ist 'bbubl'.

_________________
Pascal keeps your hand tied. C gives you enough rope to hang yourself. C++ gives you enough rope to shoot yourself in the foot
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 21.07.06 22:25 
ausblenden Delphi-Quelltext
1:
Insert(temp,s,index2+1);					

Das + 1 muss weg, sonst haste einen Buchstaben zu viel am Ende.

_________________
"Wer Ego-Shooter Killerspiele nennt, muss konsequenterweise jeden Horrorstreifen als Killerfilm bezeichnen." (Zeit.de)
Marco D. Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 2750

Windows Vista
Delphi 7, Delphi 2005 PE, PHP 4 + 5 (Notepad++), Java (Eclipse), XML, XML Schema, ABAP, ABAP OO
BeitragVerfasst: Fr 21.07.06 22:29 
Vertrau dir einfach mal, ohne das zu testen. Dachte eigentlich, der Thread sei tot. Funktioniert es ansonsten?

_________________
Pascal keeps your hand tied. C gives you enough rope to hang yourself. C++ gives you enough rope to shoot yourself in the foot
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 21.07.06 22:31 
Ich hab's ausgiebig getestet ^^ Bis jetzt geht's. Ich versuch grad zu optimieren.

_________________
"Wer Ego-Shooter Killerspiele nennt, muss konsequenterweise jeden Horrorstreifen als Killerfilm bezeichnen." (Zeit.de)
Marco D. Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 2750

Windows Vista
Delphi 7, Delphi 2005 PE, PHP 4 + 5 (Notepad++), Java (Eclipse), XML, XML Schema, ABAP, ABAP OO
BeitragVerfasst: Fr 21.07.06 22:51 
Kannst dich ja denn melden, wenn du voran kommst.

_________________
Pascal keeps your hand tied. C gives you enough rope to hang yourself. C++ gives you enough rope to shoot yourself in the foot