Delphi-PRAXiS
Seite 2 von 2     12   

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Algorithmen, Datenstrukturen und Klassendesign (https://www.delphipraxis.net/78-algorithmen-datenstrukturen-und-klassendesign/)
-   -   Delphi schnellste Variante für String Reduktion auf 1..9,0 und "." "," (https://www.delphipraxis.net/153869-schnellste-variante-fuer-string-reduktion-auf-1-9-0-und.html)

himitsu 18. Aug 2010 14:37

AW: schnellste Variante für String Reduktion auf 1..9,0 und "." ","
 
Delphi-Quellcode:
function GetNum(S: String): Extended;
var
  i: Integer;
begin
  Result := 0;
  for i := 1 to Length(S) do
    if S[i] in ['0'..'9'] then begin
      Delete(S, 1, i - 1);
      Break;
    end;
  Val(S, Result, i);
end;
und nun noch'n bissl aufgemotzt
Delphi-Quellcode:
function GetNum(S: String): Extended;
var
  i: Integer;
begin
  Result := 0;
  for i := 1 to Length(S) do
    if S[i] in ['0'..'9'] then begin
      if (i > 1) and (S[i - 1] in ['+', '-']) then
        Delete(S, 1, i - 2) else Delete(S, 1, i - 1);
      Break;
    end;
  S := StringReplace(S, ',', DecimalSeparator, [rfReplaceAll]);
  Val(S, Result, i);
end;

moelski 18. Aug 2010 15:04

AW: schnellste Variante für String Reduktion auf 1..9,0 und "." ","
 
Das wäre meine Variante:

Delphi-Quellcode:
Function Floaty(Input : String; DezSep : Char) : String;
var c         : Char;
    Thousand  : Char;
    len       : integer;
    Start     : Boolean;
    DezSepDone : Boolean;
  procedure Accept(c: char);
  begin
    inc(len);
    Result[len] := c;
  end;
begin
  len := 0; Start := False; DezSepDone := False;

  if DezSep = '.' then Thousand := ','
                  else Thousand := '.';

  SetLength(Result, Length(Input));
  for c in Input do begin
    if c in ['0' .. '9', '-', DezSep] then begin
      if (c = DezSep) and DezSepDone then Break; // Bsp : 1.323,999,34 verhindern
      Start := len=0;            // Wenn erste Zahl hinzugefügt wird -> Start
      if c = DezSep then begin
        DezSepDone := True;  // DezimalSep wurde bearbeitet
        Accept(DecimalSeparator);
      end else
        Accept(c);
    end else begin
      if Start and (c <> Thousand) then Break;
    end;
  end;
  SetLength(Result, len);
end;
Floaty('Dies ist -1,323.999.34 jojo', '.') liefert -1323,999.

jbg 18. Aug 2010 17:40

AW: schnellste Variante für String Reduktion auf 1..9,0 und "." ","
 
Zitat:

Zitat von Daniela.S (Beitrag 1043095)
SetLength( str, (Integer(p2) - Integer(str)) div 2 );

Na dann warte mal bis 64 Bit kommt, dann passt dein Integer-Typecast wieder nicht. Warum nicht einfach

Delphi-Quellcode:
SetLength( str, (p2 - PChar(str)) );
Damit passt das für ANSI, UNICODE und 64 Bit, 128 Bit, ... man weiß ja nie.

Horst_ 25. Aug 2010 11:53

AW: schnellste Variante für String Reduktion auf 1..9,0 und "." ","
 
Hallo,

@moelski:
Hänge doch bitte eine Beispieldatei an, damit man eine Tempovergleich der verschiedenen Versionen machen kann.

Gruß Horst


Alle Zeitangaben in WEZ +1. Es ist jetzt 18:25 Uhr.
Seite 2 von 2     12   

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz