Articles   Members Online: 3
-Article/Tip Search
-News Group Search over 21 Million news group articles.
-Delphi/Pascal
-CBuilder/C++
-C#Builder/C#
-JBuilder/Java
-Kylix
Member Area
-Home
-Account Center
-Top 10 NEW!!
-Submit Article/Tip
-Forums Upgraded!!
-My Articles
-Edit Information
-Login/Logout
-Become a Member
-Why sign up!
-Newsletter
-Chat Online!
-Indexes NEW!!
Employment
-Build your resume
-Find a job
-Post a job
-Resume Search
Contacts
-Contacts
-Feedbacks
-Link to us
-Privacy/Disclaimer
Embarcadero
Visit Embarcadero
Embarcadero Community
JEDI
Links
How to break strings into individual tokens (substrings) Turn on/off line numbers in source code. Switch to Orginial background IDE or DSP color Comment or reply to this aritlce/tip for discussion. Bookmark this article to my favorite article(s). Print this article
20-Oct-02
Category
Object Pascal-Strings
Language
Delphi All Versions
Views
66
User Rating
No Votes
# Votes
0
Replies
0
Publisher:
DSP, Administrator
Reference URL:
DKB
			Author: Jonas Bilinkevicius

How to break strings into individual tokens (substrings)

Answer:

The following (simple) functions helped me handling substrings:

function GetToken(aString, SepChar: string; TokenNum: Byte): string;

{Parameters:
aString: the complete string
SepChar: a single character used as separator between the substrings
TokenNum: the number of the substring you want
result: the substring or an empty string if the are less then 'TokenNum' substrings}

1   var
2     Token: string;
3     StrLen: Byte;
4     TNum: Byte;
5     TEnd: Byte;
6   begin
7     StrLen := Length(aString);
8     TNum := 1;
9     TEnd := StrLen;
10    while ((TNum <= TokenNum) and (TEnd <> 0)) do
11    begin
12      TEnd := Pos(SepChar, aString);
13      if TEnd <> 0 then
14      begin
15        Token := Copy(aString, 1, TEnd - 1);
16        Delete(aString, 1, TEnd);
17        Inc(TNum);
18      end
19      else
20      begin
21        Token := aString;
22      end;
23    end;
24    if TNum >= TokenNum then
25    begin
26      GetToken1 := Token;
27    end
28    else
29    begin
30      GetToken1 := '';
31    end;
32  end;
33  
34  function NumToken(aString, SepChar: string): Byte;
35  
36  {Parameters:
37  aString: the complete string
38  SepChar: a single character used as separator between the substrings
39  result: the number of substrings}
40  
41  var
42    RChar: Char;
43    StrLen: Byte;
44    TNum: Byte;
45    TEnd: Byte;
46  begin
47    if SepChar = ' # ' then
48    begin
49      RChar := ' * '
50    end
51    else
52    begin
53      RChar := ' # '
54    end;
55    StrLen := Length(aString);
56    TNum := 0;
57    TEnd := StrLen;
58    while TEnd <> 0 do
59    begin
60      Inc(TNum);
61      TEnd := Pos(SepChar, aString);
62      if TEnd <> 0 then
63      begin
64        aString[TEnd] := RChar;
65      end;
66    end;
67    NumToken1 := TNum;
68  end;


			
Vote: How useful do you find this Article/Tip?
Bad Excellent
1 2 3 4 5 6 7 8 9 10

 

Advertisement
Share this page
Advertisement
Download from Google

Copyright © Mendozi Enterprises LLC