Bringing the Lovely VBScript Split Function into Delphi with a Custom Function

Posted by Daniel - 2,584 Views

Split Function in DelphiIt was a very old idea that I want to be able to perform the process carried out by the VBScript Split Function with Delphi. I had done it once but only capable of doing the split process with only one character as the delimiter, if I want to split a series of strings by two or more character as the delimiter, it didn’t work. This is the update, now I can perform the Split process with more than one character as the delimiter.

Before you get confused by what the Split Function does, let me explain: Split Function which is found as a built in function on the Visual Basic family languages will slice a strings based on a character delimiter specified and then will return an array containing each pieces from the slice process.

For example if you run this code:

arrSplit = Split(“a/b/c”,“/”)

Then the arrSplit will contains 3 elements:

arrSplit(0)=”a”
arrSplit(1)=”b”
arrSplit(2)=”c”

That kind of function does not available as a built-in function in Delphi (am I wrong?), but I want to be able to perform such process with Delphi so as usual, I created my own function, and here is the code:

function MySplit(source : string; delimiter : string): TStringList;
var i : integer;
s : string;
arrString: TStringList;
begin
arrString := TStringList.Create;
s := ;
i := 1;
repeat
begin
if Copy(source,i,Length(delimiter)) <> delimiter then
begin
s := s + Copy(source,i,1);
i := i + 1;
end
else
begin
arrString.Add(s);
s := ;
i := i + Length(delimiter);
end;
end
until i > Length(source);
if s <> then arrString.Add(s);
result := arrString;
end;

MySplit Function will slice the string passed as the source parameter based on the delimiter character specified and then will return the resulting array as a stringlist.

A demo is included with this article. You can download the demo project then have it tested to see how the function works.

If you only need to split strings with one character as the delimiter, you may also consider the function from Zarko Gajic here - it’s neat and brilliant.


Download the demo project
(8 KB)

share this article

Digg del.icio.us Netscape StumbleUpon Yahoo! MyWeb reddit Furl Magnolia Newsvine Technorati SlashDot Blinklist Simpy Google
This post as PDFPosted in: Programming - February 2008

2 Responses to “Bringing the Lovely VBScript Split Function into Delphi with a Custom Function”

  1. John Taylor Says:

    Interesting blog post. What would you say was the most important marketing factor?

  2. ali Says:

    Hi
    Thank you man
    You Solve my Problem
    God Bless

    ali’s last blog post: تشیع علوی و تشیع صفوی



Leave a Reply


Options for your comment:





Hi, my name is Daniel Nugraha, a single male live on an island called Java, Indonesia. This is the place for me to share my interest in computer programming.

Coffee Break

Comments - Thanks Guys :)

  • Rangga Kusuma: Gan, Tengkiu buat postingan yang sangat berguna. Kebetulan ada project utk bikin sms gateway, dan converter Agan sangat berguna utk...
  • Chuck Norton: I actually went ahead & bolted over to Justin’s Get The Image plugin here: http://justintadlock.com/ar...
  • Chuck Norton: Question: is it possible to insert something like [custfieldimg=”joice1.jpg,15 0,1:1″] into the actual templates instead of...
  • Therese Lachance: Hi, Any idea how to have ContuttoPDF fetch the correct page language?
  • tresloukadu: yo how did u fixed when the tags shows <? and it shows < “& l t ; ” ?? please send me an email.