Author: Jonas Bilinkevicius
I need a string procedure that checks whether the caret position is between the
tags '<>' , and if this is true, the contents of the tag are placed in a string for
further processing.
Answer:
Something like this:
1 2 function FindTextBetweenTags(const S: string; caretpos: Integer): string;
3 var4 nStart, nEnd: Integer;
5 begin6 Result := EmptyStr;
7 {caretpos is 0-based, string indices are 1-based}8 Inc(caretpos);
9 {move backwards from caretpos util we find a '<'}10 nstart := caretpos;
11 while ((nstart > 0) and (S[nstart]) <> '<') do12 Dec(nstart);
13 if nstart = 0 then14 Exit;
15 {move to first char after '<'}16 Inc(nstart);
17 if S[nstart] = '>' then18 Exit; {empty tag}19 {move forward until we find a '>'}20 nend := nstart;
21 while (nend <= Length(S)) and (S[nend] <> '>') do22 Inc(nend);
23 if (nend > Length(S)) or (nend <= caretpos) then24 Exit;
25 Result := Copy(S, nstart, nend - nstart);
26 end;
You would call it like
tagstring := FindtextBetweentags(memo1.text, memo1.selstart);