Author Topic: IXSplit  (Read 1910 times)

0 Members and 2 Guests are viewing this topic.

Offline CurryWurst

  • Camper
  • ***
  • Posts: 265
    • Soldat Global Account System
IXSplit
« on: February 08, 2009, 05:52:09 am »
Script Name: IXSplit
Script Description Improved version of XSplit
Author: Dual

Hosted by: Soldat Central - http://soldatcentral.com/
Core Version: 2.6.3
Compile Test: Passed

IXSplit() splits a string into an array of strings. SOURCE determines a string to be splitted by the Delimiter.
To put it in a nutshell, it's an improved version of the common used XSplit.

Code: [Select]
function IXSplit(const SOURCE: string; Delimiter: string): array of string;
var
  i, x, d: integer;
  s, b: string;
begin
  d:= length(Delimiter);
  i:= 1;
  SetArrayLength(Result, 0);
  while (i <= length(SOURCE)) do
  begin
    s:= Copy(SOURCE, i, d);
    if (s = Delimiter) then
    begin
      SetArrayLength(Result, x + 1);
      Result[x]:= b;
      Inc(i, d);
      Inc(x, 1);
      b:= '';
    end else
    begin
      b:= b + Copy(s, 1, 1);
      Inc(i, 1);
    end;
  end;
  if (b <> '') then
  begin
    SetArrayLength(Result, x + 1);
    Result[x]:= b;
  end;
end;

Parameters:

SOURCE: string
String to be splitted into an array of string.

Delimiter: string
Defines a string as split point.

Return Value:

Depending on the amount of delimiters the SOURCE string contains, the elements count of the string array will increase.

Note:

IXSplit() will not return any array element if SOURCE is blank.
In case the delimiter is not found in the string, IXSplit() will return one array element containing the whole SOURCE string.

If you intent to split a string which is read from a file I recommend to use this function:

function ReadFromFile(File_Name: string): string;
begin
  Result:= ReadFile(File_Name);
  Result:= Copy(Result, 0, length(Result) - 2);
end;

You find more information about this problem here.



(Size 629 B)
- http://soldatcentral.com/index.php?page=script&f=100 -


** Script hosted by Soldat Central! Please visit the author's script page and Rate this script **
« Last Edit: February 17, 2009, 06:26:43 am by Markus Quär »
Soldat Global Account System: #soldat.sgas @ quakenet

Offline iDante

  • Veteran
  • *****
  • Posts: 1967
Re: IXSplit
« Reply #1 on: February 08, 2009, 10:34:39 am »
SOURCE: array of string
Array of string to be splitted into an array of string.
ORLY?

On another note, I prefer to use this:
Pretty sure it was written by Curt.
Code: [Select]
function Explode(source, delim: string): array of string;
begin
source := source + delim;
while strpos(delim, source) <> 0 do begin
SetArrayLength(Result, GetArrayLength(Result) + 1);
Result[GetArrayLength(Result) - 1] := copy(source, 0, strpos(delim, source));
source := copy(source, strpos(delim, source) + 1, length(source));
end;
end;

Offline CurryWurst

  • Camper
  • ***
  • Posts: 265
    • Soldat Global Account System
Re: IXSplit
« Reply #2 on: February 08, 2009, 10:49:53 am »
SOURCE: array of string
Array of string to be splitted into an array of string.
ORLY?

Oops, thanks for hint :)
Actually it should be:

SOURCE: string
String to be splitted into an array of string.

Code: [Select]
function Explode(source, delim: string): array of string;
begin
source := source + delim;
while strpos(delim, source) <> 0 do begin
SetArrayLength(Result, GetArrayLength(Result) + 1);
Result[GetArrayLength(Result) - 1] := copy(source, 0, strpos(delim, source));
source := copy(source, strpos(delim, source) + 1, length(source));
end;
end;

Nice clear function. I've never seen it before.
Seems to work quite well. Good job Curt! :D
Soldat Global Account System: #soldat.sgas @ quakenet

Offline DorkeyDear

  • Veteran
  • *****
  • Posts: 1507
  • I also go by Curt or menturi
Re: IXSplit
« Reply #3 on: February 08, 2009, 11:00:04 am »
Just to verify, yeah that is mine. (looks like slight change of looks though, lol)