Delphi is a nice language in that it is not case sensetive. However, I would still encourage you to stick to one casing. Decide on if you want to use
If and stick to that. (My personal opinion: Use all lowercase). This also goes for
for in your code.
Your code has a bit of inconsistent spacing. Take a look at this line for example:
I would write this as:
Result[NTokens] := Result[NTokens] + Chain[i];
I also think that you can use more spaces in your function header for
GetToken (compare it with
Validate). And also use a space after each comma (in parameter lists and variable declarations).
Validate method is quite good. I agree with ChrisWue that the naming could be better.
As for your GetToken method, that can be drastically improved.
Instead of appending char-by-char to the
Result[NTokens] string, consider using a
var Current: String, add the chars to that string and then when you encounter the next token separator you add the
Current to the result.
Regarding your Result, instead of using an array of String, consider the class
Speaking of TStringList, that actually has support for using delimiters. See this SO question for an example.
If you want to do this yourself (which can be good practice), I would suggest returning a
TStringList and calling the add method to add new strings to it. Here's a start for you on returning TStringList, as it's been a while since I used Delphi you will have to fix my mistakes and possibly change some stuff to make it work.
Result := TStringList.Create;
Current := "";
for i := 1 to Length(Chain) do
if IsTokenDelimiter(Chain[i]) then
if (Length(Current) > 0) then
Current = "";
Current = Current + Chain[i];
If you want to learn more about using built-in systems, take a look around at the TStringList class and see if it's delimiter system can simplify things for you. Perhaps it can't, in which case you might want to google for other Delphi classes that can. It's been a while since I've used Delphi, so I can't recommend a specific something that I know would work for you.
Lastly, I would like you to think about why you need this feature. Where does your string come from? If it's input from the user, then it's probably fine (although you might want to seperate the user inputs). If it is result from a function that you wrote, use a record, an object or a class type. Really, learn how to write your own records and objects and classes! They will simplify many things for you.