Jump to content
xisto Community
Sign in to follow this  
Forsaken

Simple Parsing Functions [Delphi] Tutorial

Recommended Posts

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:

 

function TForm1.SimpleParse(MainString, BeginString, EndString: string): string;varPosBeginString: integer;PosEndString: integer;beginPosBeginString := Pos(BeginString, MainString) + Length(BeginString);PosEndString := Pos(EndString, MainString);Result := Copy(MainString, PosBeginString, PosEndString - PosBeginString);end;

-----------

For example, the 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

iStart = InStr(1, MainString, BeginString, vbTextCompare) + Len(BeginString)iEnd = InStr(iStart, MainString, EndString, vbTextCompare)sText = Mid$(MainString, iStart, iEnd - iStart)

----

Delphi Parsing

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:

 

Edited by snlildude87 (see edit history)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.