954,529 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Parsing a string. Need help

Hello,
I am new to delphi. I have a problem. I have a string which can be of any length. Sometimes with delimiter characters for printing in new line.
Now I need to distinguish whether the received string is a number (both -ve and +ve) or any other alpha numeric value. Based on that I need to proceed further.
Any built in function to check this? or how to parse and find out its a number?

regards,
Anand

anandkrishnantc
Newbie Poster
10 posts since Mar 2009
Reputation Points: 10
Solved Threads: 0
 

There are a number of conversion functions the sysutils.pas unit that can test this for you and check out extractstrings in the Classes unit for a basic parsing function.

jsosnowski
Junior Poster in Training
68 posts since Nov 2007
Reputation Points: 11
Solved Threads: 11
 

I'm not good in delphi...but I want to help you
I looked for you problem on the web,and there was a good idea for your problem.They say:Try to convert that string into number.The StrToIntDef takes an extra argument that is returned, if the string could not be converted to int successfully.

FlamingClaw
Posting Pro
559 posts since Feb 2009
Reputation Points: 132
Solved Threads: 138
 

Or a simple try... except...end block such as

function IsInteger(MyString:string):boolean;
begin
  try
    Result:=true;
    strtoint(MyString);
  except
    Result:=false;
  end;
end;


IntToStrDef sounds easier. ;)

m610
Light Poster
41 posts since Oct 2008
Reputation Points: 10
Solved Threads: 1
 

By the way, I had not known about the StrToIntDef function until reading this thread. I checked with Help in my copy of Delphi 6, which is where I have learned most of my Delphi programming, and it did not list it. Then I looked in SysUtils.pas and found it, along with some other nice functions I wish I had known about, such as:

function StrToFloatDef(const S: string; const Default: Extended): Extended;

and

function TryStrToFloat(const S: string; out Value: Extended): Boolean;

I'll be digging around in there a bit more for other things I have missed, like the string-to-int function that converts strings, including hex-formatted strings, to integers.

m610
Light Poster
41 posts since Oct 2008
Reputation Points: 10
Solved Threads: 1
 

Hello, thanks for the replies. I have a text (Widestring). Which need to be checked (no need or converting it ), if its a float or not. When I tried TexttoFloat, the program behaves worgly. No proper ending of the method.
and I see all the text ends with #0 . should I remove this trailing #0 ?

anandkrishnantc
Newbie Poster
10 posts since Mar 2009
Reputation Points: 10
Solved Threads: 0
 

Or a simple try... except...end block such as

function IsInteger(MyString:string):boolean;
begin
  try
    Result:=true;
    strtoint(MyString);
  except
    Result:=false;
  end;
end;

IntToStrDef sounds easier. ;)

It worked. thanks.

anandkrishnantc
Newbie Poster
10 posts since Mar 2009
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You