Ignore Character in String

JBALES
JBALES used Ask the Experts™
on
In the code below I check if a certain value is greater than 0, but when there is a comma in the integer I receive an error. How can I ignore the comma?

begin
  if (StrToInt(ListView1.Items[0].Caption)) > 0 then
    ListView1.Canvas.Font.Color := clGreen
  else
    ListView1.Canvas.Font.Color := clRed;
end;

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Expert of the Quarter 2010
Expert of the Year 2010
Commented:
begin
  if (StrToInt( StringReplace(ListView1.Items[0].Caption, ',', '', [rfReplaceAll]) )) > 0 then
    ListView1.Canvas.Font.Color := clGreen
  else
    ListView1.Canvas.Font.Color := clRed;
end;
Expert of the Quarter 2010
Expert of the Year 2010

Commented:
or since you are only testing against "0" !

begin
  if ListView1.Items[0].Caption <> '0' then
    ListView1.Canvas.Font.Color := clGreen
  else
    ListView1.Canvas.Font.Color := clRed;
end;

assuming there are no negative numbers
I do not know delphi, but I think before converting from string to int, why not get rid of the , if any present

 Our VariableString = OurVariableString.Replace(',', ' ')

I found this web article:

http://www.delphibasics.co.uk/RTL.asp?Name=StringReplace

But I might be wrong as I dont know Delphi. I think that you are getting error as commas is becoming some thing after STRINGtoInt conversion that is not an integer. What do you think?

Author

Commented:
Yes, there are negative numbers at times.

@ cyberkiwi - Your example helped, thank you. But how can I go about checking if every item if it is greater or less than 0 every time one is added? The font color stays green in the whole column 0 for even integers that are less than 0.

Author

Commented:
Point increase +

Author

Commented:
@ ashley2009 - Thanks for your input, but I needed for the comma to stay in place and to just be ignored in the comparison.
Expert of the Quarter 2010
Expert of the Year 2010
Commented:
Use a CustomDrawItem event.

procedureTForm1.ListView1CustomDrawItem(
  Sender: TCustomListView; Item: TListItem; State: TCustomDrawState;
  var DefaultDraw: Boolean);
begin
  if (StrToInt( StringReplace(Item.Caption, ',', '', [rfReplaceAll]) )) > 0 then
    ListView1.Canvas.Font.Color := clGreen
  else
    ListView1.Canvas.Font.Color := clRed;
end;

Author

Commented:
Thank you :)

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial