Autor Beitrag
alias5000
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 2145

WinXP Prof SP2, Ubuntu 9.04
C/C++(Code::Blocks, VS.NET),A51(Keil),Object Pascal(D2005PE, Turbo Delphi Explorer) C# (VS 2008 Express)
BeitragVerfasst: Sa 24.12.05 11:51 
Hi
Ein ganz einfaches Problem, bei dem ich einfach nix besseres gefunden habe, obwohl ich mir sicher bin, dass es das hier schon gibt...
Komisch, naja egal.
Es geht darum, dass ich einen (Teil-)String überprüfen will, ob er eine Zahl ist.
Da gibt es ja die schöne nette, aber unsaubere, Programmierlösung via try-except:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
try
  strtoint(TheString)
except
  //das teil ist keine Ganzzahl
end;


Die Sache ist bloß die, dass es nervt, wenn das Programm ständig (und das ist bei mir durchaus im 5 Sekundentakt) von Delphi unterbrochen wird, weil eine Exception auftritt.
Deshalb:
Ich suche eine Funktion, die mir überprüft, ob ein String eine Zahl ist, oder nicht, ohne das über Try-except gegangen wird, oder sonst irgendwie eine Exception ausgelöst wird.

Gruß alias5000

_________________
Programmers never die, they just GOSUB without RETURN
Alstar
ontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic star
Beiträge: 827



BeitragVerfasst: Sa 24.12.05 11:52 
Hallo!

Ich glaube das dürfte dir weiterhelfen: Suche in: Delphi-Forum TRYSTRTOINT

Alstar
alias5000 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 2145

WinXP Prof SP2, Ubuntu 9.04
C/C++(Code::Blocks, VS.NET),A51(Keil),Object Pascal(D2005PE, Turbo Delphi Explorer) C# (VS 2008 Express)
BeitragVerfasst: Sa 24.12.05 12:26 
danke! Sowas hab ich gesucht, jetzt gehts endlich mal etwas besser.

Gruß alias5000

_________________
Programmers never die, they just GOSUB without RETURN
Horst_H
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 1654
Erhaltene Danke: 244

WIN10,PuppyLinux
FreePascal,Lazarus
BeitragVerfasst: Sa 24.12.05 14:02 
Hallo,

ist die Prozedur Val nicht mehr funktionstüchtig?
www.delphi-forum.de/viewtopic.php?t=51644

Gruss Horst
Alstar
ontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic star
Beiträge: 827



BeitragVerfasst: Sa 24.12.05 14:07 
Doch. Aber warum sollte er sie nehmen? Val ist imho nur dann besser als TryStrToInt, wenn man wissen möchte, an welcher Stelle StrToInt scheitert.
So wie ich user profile iconalias5000 verstanden habe, möchte er aber nur überprüfen, ob es eine Zahl ist, oder nicht.

Alstar
Christian S.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 20451
Erhaltene Danke: 2264

Win 10
C# (VS 2019)
BeitragVerfasst: Sa 24.12.05 14:12 
Hier mal die Implementation der Funktion TryStrToInt aus der SysUtils.pas:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
function TryStrToInt(const S: string; out Value: Integer): Boolean;
var
  E: Integer;
begin
  Val(S, Value, E);
  Result := E = 0;
end;


Bei der Verwendung von Val spart man sich also einen Funktionsaufruf gegenüber der Verwendung TryStrToInt.

_________________
Zwei Worte werden Dir im Leben viele Türen öffnen - "ziehen" und "drücken".
alias5000 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 2145

WinXP Prof SP2, Ubuntu 9.04
C/C++(Code::Blocks, VS.NET),A51(Keil),Object Pascal(D2005PE, Turbo Delphi Explorer) C# (VS 2008 Express)
BeitragVerfasst: Sa 24.12.05 14:13 
ja mir reicht die erste.
Der Hintergrund ist folgender:
Ich bekomme einen Befehl eines Chatprotokolls als String.
Ältere Versionen haben einfach den Namen des Befehls ganz vorne.
Neuere Versionen haben vor dem Befehl noch eine Zahl, die die Protokollversion des Befehls kennzeichnet.

Als beispiel:

Altes Protokoll: "MESSAGE|...[Parameter]"
Neues Protokoll: "16CMESSAGE|...[Parameter]" (Warum da noch ein C von message steht, ist jetzt egal, ist nix zum Thema)

in zukünftigen Protokollversionen wird dann halt sowas etwa folgen:
"17CMESSAGE|...[Parameter]"

und dies galt es zu unterscheiden. die ersten zwei Zeichen hab ich mir so oder so schon mit copy abgetrennt, von daher ist es einfacher, das try-except Konstrukt durch trystr... zu ersetzen, als sich jetzt noch die Arbeitsweise von Val anzueignen, obwohl dies vllcht, wenn man sie von Anfang an genommen hätte, besser gewesen wäre.

Gruß alias5000

_________________
Programmers never die, they just GOSUB without RETURN