Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Facebook Sign In with Google Sign In with OpenID

Categories

We have migrated to a new platform! Please note that you will need to reset your password to log in (your credentials are still in-tact though). Please contact lee@programmersheaven.com if you have questions.
Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

help with strings

GladeGlade Posts: 48Member
Hi there. I have just studied strings and i have encountered a problem.
I have an htmlfile (.html) with some tags. Now, The specification indicates that a tag should always begin with a < on the first character of a line. It follows that if you find spaces before the '<' you should report an error. Any spaces on the right-hand side should be removed. I know i have to use the pos() function but i couldn't get heads or tails from where to start. Can some1 tell me how to do this?

Thanks
Glade


Comments

  • jasonpsagejasonpsage Posts: 37Member
    : Hi there. I have just studied strings and i have encountered a problem.
    : I have an htmlfile (.html) with some tags. Now, The specification indicates that a tag should always begin with a < on the first character of a line. It follows that if you find spaces before the '<' you should report an error. Any spaces on the right-hand side should be removed. I know i have to use the pos() function but i couldn't get heads or tails from where to start. Can some1 tell me how to do this?
    :
    : Thanks
    : Glade
    :
    :
    :
    Well, Glade, I'm not exactly Sure Why you can't make heads or tails of the POS function but I'll try to give you some alternative ones that MIGHT help you. I don't claim that these are the most efficient ways to do what they do, but they work for me..so far just fine. You'll see I use ANSISTRINGS (This is tested on 1.0.6 FreePascal from www.freepascal.org which is compatible with most of Borland so I hope this helps)
    FIRST ROUTINE TO HELP YOU (is basically my version of POS):
    CUT BEGIN ------------------------------------------------------------
    [code]
    //=============================================================================
    function i4InStr(p_saSearchMe: AnsiString; p_chSearchFor: char): LongInt;
    //=============================================================================
    // return index to first position where p_chSearchFor is found 0=no find
    //=============================================================================
    var i4Pos: LongInt;
    begin
    i4InStr:=0;
    if length(p_saSearchMe)>0 then
    begin
    i4Pos:=0;
    repeat
    i4Pos+=1;
    if p_saSearchMe[i4Pos]=p_chSearchFor then
    begin
    i4InStr:=i4Pos;
    i4Pos:=0;
    end;
    until (i4Pos=0) or (i4Pos=length(p_saSearchMe));
    end;
    end;
    //=============================================================================
    [/code]
    CUT END ------------------------------------------------------------





    -=/Here Is A Routine to help Strip Spaces or whatever/=-
    CUT BEGIN ------------------------------------------------------------
    [code]
    // remove a chunk out of a string p_s: start of cut, p_l:length of cut
    //*****************************************************************************
    function saCut(p_sa: AnsiString; p_s, p_l: LongInt):AnsiString;
    //*****************************************************************************
    var saTempLeft: AnsiString;
    saTempRight: AnsiString;
    t: LongInt;
    begin
    saCut:=p_sa;
    if (length(p_sa)>0) and (p_s<=length(p_sa)) and
    ((p_s+p_l-1)<=Length(p_sa)) and (p_l<>0) then
    begin
    if p_s>1 then
    begin
    for t:=1 to p_s-1 do saTempLeft:=saTempLeft+p_sa[t];
    end
    else
    begin
    saTempLeft:='';
    end;
    if (p_s+p_l-1)<length(p_sa) then
    begin
    for t:=(p_s+p_l) to length(p_sa) do saTempRight:=saTempRight+p_sa[t];
    end
    else
    begin
    saTempRight:='';
    end;
    saCut:=saTempLeft+saTempRight;
    end;
    end;
    //*****************************************************************************
    [/code]
    CUT END ------------------------------------------------------------









  • GladeGlade Posts: 48Member
    thanks for the help but I would want to use the pos() function for this so that I'd know how to do it. Thanks
    Glade
  • zibadianzibadian Posts: 6,349Member
    : Hi there. I have just studied strings and i have encountered a problem.
    : I have an htmlfile (.html) with some tags. Now, The specification indicates that a tag should always begin with a < on the first character of a line. It follows that if you find spaces before the '<' you should report an error. Any spaces on the right-hand side should be removed. I know i have to use the pos() function but i couldn't get heads or tails from where to start. Can some1 tell me how to do this?
    :
    : Thanks
    : Glade
    :
    Here is a simple code to report the errors in the HTML and remove the spaces. This code takes just 1 line of HTML. You can easily adapt this code to process multiple lines, by for example creating a procedure from it.
    [code]
    begin
    if Pos('<', Line) > 1 then { If position in line is not 1st character }
    writeln('Error in line');
    while Line[Length(Line)]=' ' do { while any spaces at the end of the string }
    delete(Line, Length(Line), 1); { remove it }
    end;
    [/code]
Sign In or Register to comment.