Einzelnen Beitrag anzeigen

Volker Z.

Registriert seit: 3. Dez 2012
Ort: Augsburg, Bayern, Süddeutschland
419 Beiträge
 
Delphi XE4 Ultimate
 
#5

AW: Anfang und Ende eines String teils ermitteln

  Alt 3. Mär 2013, 23:18
Hallo,

Zitat:
Aber bei beiden funktionen kommt als Ergebnis immer eine ca. 7 stellige zahl und ich weis nicht warum das so ist.
Immer glaube ich jetzt nicht. Wenn man die beiden Funktion mit dem Startwert 16 laufen lässt, denn kommen - wie KWolf bereits anmerkte - korrekte Werte raus. Mit dem Startwert 41 verhält es sich allerding nicht so. Warum? Es ist das letzte Wort in Deinem Teststring, und da kommt halt dann kein Leerzeichen mehr; aber Du suchst fröhlich über die Stringgrenzen hinaus weiter, solange bis dann halt zufällig mal im Speicher ein Leerzeichen auftaucht. Gleiches gilt für einen Startwert der im ersten Wort liegt.

Zitat:
So ich habe das Problem gelöst, war ein Logik Fehler von mir während des aufrufens der Funktion.
Galube ich nicht. Siehe oben.


Du kannst ja mal folgendes versuchen:
Delphi-Quellcode:
unit Unit2;

interface
  function StringPartEnd (const Str : string; const Start : Integer) : Integer;
  function StringPartStart (const Str : string; const Start : Integer) : Integer;

implementation

function CheckStartInRange (const Str : string; const Start : Integer) : Integer;
begin
  if (Start < 1) or (Length (Str) < Start) then
    Result := 0
  else
    Result := Start
end;

function IsWhitespace (const Str : string; const Start : Integer) : Boolean;
begin
  Result := CharInSet (Str [Start], [#9, #10, #13, #32])
end;

function StringPartStart (const Str : string; const Start : Integer) : Integer;
begin
  Result := CheckStartInRange (Str, Start);
  if Result = 0 then
    Exit;

  if IsWhitespace (Str, Result) then
    Exit;

  while (Result > 1) and (Str [Result - 1] <> ' ') do
    Dec (Result)
end;

function StringPartEnd (const Str : string; const Start : Integer) : Integer;
var
  l : Integer;
begin
  Result := CheckStartInRange (Str, Start);
  if Result = 0 then
    Exit;

  if IsWhitespace (Str, Result) then
    Exit;

  l := Length (Str);
  while (Result < l) and (Str [Result + 1] <> ' ') do
    Inc (Result)
end;

end.
und in Deinem Form
Delphi-Quellcode:
procedure TForm1.Button1Click(Sender: TObject);
var
  s : string;
  i, j : Integer;
begin
  s := 'Das ist der String den ich untersuchen möchte';

  i := StringPartStart (s, 41);
  j := StringPartEnd (s, 41);
  Memo1.Lines.Add (Format ('%d %d %s', [i, j, Copy (s, i, j - i + 1)]));
  //Ausgabe 40 45 möchte


  i := StringPartStart (s, 2);
  j := StringPartEnd (s, 2);
  Memo1.Lines.Add (Format ('%d %d %s', [i, j, Copy (s, i, j - i + 1)]));
  // Ausgabe 1 3 Das
 
  i := StringPartStart (s, 16);
  j := StringPartEnd (s, 16);
  Memo1.Lines.Add (Format ('%d %d %s', [i, j, Copy (s, i, j - i + 1)]));
  // Ausgabe 13 18 String

  i := StringPartStart (s, 4);
  j := StringPartEnd (s, 4);
  Memo1.Lines.Add (Format ('%d %d %s', [i, j, Copy (s, i, j - i + 1)]));
  // Ausgabe 4 4
end;
Gruß
Volker Zeller

Geändert von Volker Z. ( 3. Mär 2013 um 23:20 Uhr)
  Mit Zitat antworten Zitat