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
: 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 ------------------------------------------------------------
Glade
: 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]