Autor Beitrag
Hänsel
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 144



BeitragVerfasst: Mi 12.10.16 11:06 
Hallo ich benötige wieder einmal Hilfe,

ich arbeite mit Delphi X2 und möchte in der Listbox bei der Pfadangabe nicht die gesamte Länge (152 Berlin Hauptstraße 24') des Pfads angeben sondern nur den ersten numerischen Teil wie z.B. 152.....
Wer kann mir hier weiter helfen?


fn:='Z:\Objekt_Schrift/152 Berlin Hauptstraße 24';
DirectoryListBox1.Directory:=fn;

Danke Hänsel
Nersgatt
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 1581
Erhaltene Danke: 279


Delphi 10 Seattle Prof.
BeitragVerfasst: Mi 12.10.16 11:14 
Du kannst mit Pos oder PosEx die Position von bestimmten Zeichen in einem String ermitteln.
Mit Copy kannst Du dann bestimmte Teile des String heraus kopieren.

Wenn der gesuchte Teil also immer hinter dem / steht, ermittle die Position des /. Dann ermittle ab der Position die Position der Leerstelle. Alles was dazwischen steht, kopierst Du raus.

_________________
Gruß, Jens
Zuerst ignorieren sie dich, dann lachen sie über dich, dann bekämpfen sie dich und dann gewinnst du. (Mahatma Gandhi)
Hänsel Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 144



BeitragVerfasst: Mi 12.10.16 12:04 
Danke für die Info. Kannst Du mir eventuell ein Beispiel geben wie ich das zur Laufzeit einbinden kann.
Nersgatt
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 1581
Erhaltene Danke: 279


Delphi 10 Seattle Prof.
BeitragVerfasst: Mi 12.10.16 13:31 
Du musst zwischen deinen beiden Zeilen den entsprechenden Teil raus kopieren:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
var
  PositionSlash, PositionLeerstelle : integer;

begin

fn:='Z:\Objekt_Schrift/152 Berlin Hauptstraße 24'

PositionSlash := Pos(...
PositionLeerstelle := PosEx(...
fn := Copy(fn, ...

DirectoryListBox1.Directory:=fn;

_________________
Gruß, Jens
Zuerst ignorieren sie dich, dann lachen sie über dich, dann bekämpfen sie dich und dann gewinnst du. (Mahatma Gandhi)
t.roller
ontopic starontopic starontopic starontopic starontopic starofftopic starofftopic starofftopic star
Beiträge: 118
Erhaltene Danke: 34



BeitragVerfasst: Mi 12.10.16 18:30 
delphiexamples.com/f...es/getshortpath.html
ausblenden volle Höhe 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:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
function GetShortPath(Path: string; Count: Integer): string;

  procedure Slashes(var Str: stringvar Num: Integer);
  var
    Position, Index: Integer;
  begin
    Index:=0;
    repeat
      Position:=Pos('\', Str);
      Delete(Str,1,Position);
      if Position<>0 then Inc(Index);
      if (Index=Num)and(Num<>0then break;
    until Position=0;
    Num:=Index;
  end;

var
  Num, NewNum, P: Integer;
  Str: string;
begin
  Str:=Path;
  Num:=0;
  Slashes(Path, Num);
  while (Length(Str)>Count)and(Num>2do
  begin
    NewNum:=Num div 2;
    Path:=Str;
    Slashes(Path, NewNum);
    P:=Pos(Path, Str);
    Delete(Str,P, Length(Path));
    NewNum:=2;
    Slashes(Path, NewNum);
    Str:=Str+'...\'+Path;
    Dec(Num);
  end;
  Result:=Str;
end;

procedure TForm1.ButtonClick(Sender: TObject);
var MEDIA : String;
begin
   MEDIA:= 'F:\@_MP3\@_MP3-1\Daliah Lavi - Very Best of\08 - Daliah Lavi - Oh wann kommst du.mp3';
   Label1.Caption:= GetShortPath(MEDIA,30); // nach Belieben verkürzen
end;

Ausgabe:
F:\...\08 - Daliah Lavi - Oh wann kommst du.mp3