#1
  1. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    May 2002
    Location
    UK
    Posts
    563
    Rep Power
    13

    checking string for number


    I have a problem which may be simple, but I am not sure how to get around it. I have a string where it has a two digit representation for the year. I need to make this 4 digit and am using the following to check which year prefix to put in front. unfortunatly some lines of this text do not have numbers. How would I check this before doing the following function?
    Code:
    Begin
    if StrtoInt(copy(S,28,2))< 79 then  begin
    YearPrefix := '20';
    end else
    begin
    YearPrefix := '19';
    end;
  2. #2
  3. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,640
    Rep Power
    4247
    One way is to use a try...except block.
    Code:
    try
      if StrtoInt(copy(S,28,2))< 79 then  begin
        YearPrefix := '20';
      end else
      begin
        YearPrefix := '19';
      end;
    except 
      on E: EConvertError do
          ShowMessage('This line doesn't have a number');
    end;
    Another way is to use StrToIntDef instead of StrToInt. The StrToIntDef takes an extra argument that is returned, if the string could not be converted to int successfully.
    Code:
    var
       sTemp :string;
       nTemp : integer;
    
    sTemp := copy(S,28,2);
    nTemp = StrToIntDef(sTemp, -999); // -999 is returned if sTemp cannot be converted to int.
    
    if nTemp = -999 then
        ShowMessage('You have an error in S')
    else if nTemp < 79 then  begin
        YearPrefix := '20';
    end else
    begin
        YearPrefix := '19';
    end;
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    May 2002
    Location
    UK
    Posts
    563
    Rep Power
    13
    Thanks for that scorpions. Solution 1 did'nt work but 2 was fine.

IMN logo majestic logo threadwatch logo seochat tools logo