Home  |  Delphi .net Home  |  system.String  |  Split Method
Split  
Method  
Parses a string into an array of substrings
String Class
system NameSpace
CF1.  Function Split ( CharArray : Array of Char; ) : Array of String ;
NotCF2.  Function Split ( CharArray:Array of CharCharArray : Array of Char; Count : Integer; ) : Array of String;
CF : Methods with this mark are Compact Framework Compatible
Description
The Split method is intended to work as a simple string parser. It scans for separating characters, extracting the text either side of the separator into substrings.
 
The number of substrings can be limited by the Count parameter.
 
For example, the following string :
 
Age=47;Name=Neil;Occupation=Programmer
 
Can be parsed first using ';' as the separator character to give :
 
Age=47
 
Name=Neil
 
Occupation=Programmer
 
And then each of these can be parsed using the '=' separator character.
 
Or the original string can be parsed in one go by providing both the ';' and '=' characters in the character array parameter.
 
the second example illustrates this parsing in one run of split. Be careful doing this in one go - it makes the process more error prone!
Microsoft MSDN Links
system
system.String
 
 
A simple example
program Project1;
{$APPTYPE CONSOLE}

var
  strA      : String;
  charArray : Array[0..0] of Char;
  strArray  : Array of String;

  i         : Integer;

begin
  strA         := 'One/Two/Three';
  charArray[0] := '/';
  strArray     := strA.Split(charArray);

  Console.WriteLine('strA = ' + strA);

  Console.WriteLine('strArray strings :');

  for i := 0 to Length(strArray)-1 do
    Console.WriteLine(strArray[i]);

  Console.ReadLine;
end.
Show full unit code
  strA = One/Two/Three
  strArray strings :
  One
  Two
  Three
Multiple character parsing
program Project1;
{$APPTYPE CONSOLE}

var
  strA      : String;
  charArray : Array[0..1] of Char;
  strArray  : Array of String;

  i         : Integer;

begin
  strA         := 'Age=47;Name=Neil;Occupation=Programmer';

  charArray[0] := ';';
  charArray[1] := '=';

  strArray     := strA.Split(charArray);

  Console.WriteLine('strA = ' + strA);

  Console.WriteLine('strArray strings :');

  for i := 0 to Length(strArray)-1 do
    Console.WriteLine(strArray[i]);

  Console.ReadLine;
end.
Show full unit code
  strA := Age=27;Name=Neil;Occupation=Programmer
  strArray strings :
  Age
  47
  Name
  Neil
  Occupation
  Programmer
 
 
Delphi Programming © Neil Moffatt All rights reserved.  |  Contact the author