Because parsing is such an integral part of string manipulation, I took the time to make a quick and simple parse function.

For you VBers, Delphi is a derivative of Pascal...it's powerful, simple, and (best of all) doesn't need external components (eg: ocx files).

Hence the code:

CODE

function TForm1.SimpleParse(MainString, BeginString, EndString: string): string;

var

PosBeginString: integer;

PosEndString: integer;

begin

PosBeginString := Pos(BeginString, MainString) + Length(BeginString);

PosEndString := Pos(EndString, MainString);

Result := Copy(MainString, PosBeginString, PosEndString - PosBeginString);

end;


-----------
For example, the code:
CODE

ShowMessage(SimpleParse('This is a delphi parsing tutorial.', 'delphi', 'tutorial'));

--------------

...will return the string: 'parsing'

Notes:
1) The instance variables PosBegin/EndString aren't needed if you replace their assigned values in the actual Copy function.

2) You don't necessarily need the function to parse. You can take those three lines (or single line) of code and put it in a procedure.

That is all. I may make a parsing function that parses multiple items in the future.

Enjoy.


----
Im more familiar with VB, but once I began learning Delphi I realized they are very much alike, checkout the comparison below

----
Visual Basic 6 Parsing
CODE

iStart = InStr(1, MainString, BeginString, vbTextCompare) + Len(BeginString)

iEnd = InStr(iStart, MainString, EndString, vbTextCompare)

sText = Mid$(MainString, iStart, iEnd - iStart)


----
Delphi Parsing
CODE

PosBeginString := Pos(BeginString, MainString) + Length(BeginString);

PosEndString := Pos(EndString, MainString);

Result := Copy(MainString, PosBeginString, PosEndString - PosBeginString);



-----------------------------
Hope you enjoy! Look for Advanced Parsing Function tutorial soon...
Will post as soon as I get time to write it.

Thanks

Remember to place all code in code tags:
QUOTE
[code][/code]

 

 

 


Reply