Einzelnen Beitrag anzeigen

jacky213

Registriert seit: 2. Mär 2011
146 Beiträge
 
#7

AW: Anfang und Ende eines String teils ermitteln

  Alt 4. Mär 2013, 08:29
Der Übersicht halber hier nun das Endergebnis:

Die Komplette Unit mit Aufruf:
Delphi-Quellcode:
unit StringChecker;

interface
  function StringPartEnd (const Str : string; const Start : Integer) : Integer;
  function StringPartStart (const Str : string; const Start : Integer) : Integer;
  function CopyWord(Text: String; Position: integer) : String;

implementation

uses unit1, SysUtils;

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;

function CopyWord(Text: String; Position: integer) : String;
var
i, j: integer;
begin
i := StringPartStart (Text, Position);
j := StringPartEnd (Text, Position);
Result := Copy (Text, i, j - i + 1);
end;

end.
Und hier nun der Aufruf:
Delphi-Quellcode:
procedure TForm1.Button15Click(Sender: TObject);
var
s: string;
begin
S:= 'Das ist der String den ich testen möchte';
showmessage(CopyWord(S, 15));
end;
Die Ausgabe ist in diesem Fall dann:
String

Danke nochmal an Volker Z. und KWolf

Geändert von jacky213 ( 4. Mär 2013 um 08:35 Uhr)
  Mit Zitat antworten Zitat