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

    Join Date
    May 2002
    Location
    UK
    Posts
    563
    Rep Power
    13

    Checking the begining of a string for a character


    Just a quick one,
    I need to check the beginning of a string for a certain character (G) and if so keep the string otherwise it should be nothing.
    Is the following code the right way to go about it?

    Code:
    Function GrpNum (S: String): String; stdcall;
    begin
    Result := copy(S,1,1);
    if  Result = 'G' then
    Result := copy(S,1,5);
    Result := StringReplace(Result, ' ', '', [rfReplaceAll]);
    else
    Result := '';
    end;
    Cheers
  2. #2
  3. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,607
    Rep Power
    4247
    This is a good reason why you should always indent your code well. You're missing one set of begin and end statements, which causes the else to not match the if. With code indentation, it is easy to see where the problem is.
    Code:
    Function GrpNum (S: String): String; stdcall;
    begin
        Result := copy(S,1,1);
        if  Result = 'G' then
        begin
            Result := copy(S,1,5);
            Result := StringReplace(Result, ' ', '', [rfReplaceAll]);
        end
        else
             Result := '';
    end;
    BTW, if you just want to keep the whole string, why not just do this:
    Code:
    begin
        if Copy(S, 1, 1) = 'G' then
             Result := Trim(S)
        else
             Result := '';
    end;
    The Trim() function removes leading and trailing spaces.
    Last edited by Scorpions4ever; April 10th, 2004 at 12:26 PM.
    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
    Scorpions thanks for the tip. I can not use trim because I am only using part of a string.

    Lloyd

IMN logo majestic logo threadwatch logo seochat tools logo