|
| | Terms of Agreement:
By using this article, you agree to the following terms...
1) You may use
this article in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
2) You MAY NOT redistribute this article (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.
3) You may link to this article from another website, but ONLY if it is not wrapped in a frame.
4) You will abide by any additional copyright restrictions which the author may have placed in the article or article's description. | //Declare private string for the unit
implementation
var
TokS: string
//Function To Get Characters From The String
function StrMid(const sData: string; nStart: integer; nLength: integer): string; overload;
begin
Result := copy(sData, nStart, nLength);
end;
//Function To Get Characters From The String
function StrMid(const sData: string; nStart: integer): string; overload;
begin
Result := copy(sData, nStart, Length(sData) - (nStart - 1));
end;
//String Tokenizer function
function StrTok(S: string; Tok: string = ''): string;
var
i,
LenTok: integer;
begin
if not(S = '') then begin //New String
TokS := S;
result := '';
Exit;
end;
if Tok = '' then begin //If No Token
result := TokS;
Exit;
end;
LenTok := Length(Tok);
for i := 0 to Length(TokS) - LenTok do begin
if StrMid(TokS, i, LenTok) = Tok then begin //If Token Found
result := StrMid(TokS, 1, i - LenTok);
TokS := StrMid(TokS, i + LenTok + 1);
Exit;
end;
end;
//If Program Reaches Here, Return The Rest Of The String
result := TokS;
TokS := '';
end;
Therefore, for example, if you defined a string:
var
S: string
and set S to 'Hello World'
S := 'Hello World';
and then call StrTok() like so:
StrTok(S);
The procedure will store the string, from now on, calling StrTok() with no S value and a token (such as a space ' '), the program will return everything in the string up to the token.
EX:
var
firstWord,
secondWord: string
begin
StrTok(S);
firstWord := StrTok('', ' ');
secondWord := StrTok('', ' ');
end;
//Doing this will break apart the string S at the token (' ') into the first word and the second word.
Good luck and use this code well!
| | Other 2 submission(s) by this author
| | | Report Bad Submission | | | Your Vote! |
See Voting Log | | Other User Comments | There are no comments on this submission. | | Add Your Feedback! | Note:Not only will your feedback be posted, but an email will be sent to the code's author in your name.
NOTICE: The author of this article has been kind enough to share it with you. If you have a criticism, please state it politely or it will be deleted.
For feedback not related to this particular article, please click here. | | |