Home  |  Delphi .net Home  |  system.String  |  ToCharArray Method
ToCharArray  
Method  
Creates a character array from a string
String Class
system NameSpace
CF1.  Function ToCharArray ( ) : Array of Char ;
NotCF2.  Function ToCharArray ( Start:IntegerStart : Integer; Count : Integer; ) : Array of Char;
CF : Methods with this mark are Compact Framework Compatible
Description
Builds an array of characters from the current string.
 
Optionally, the Start index in the string, and the Count of characters can be specified.
Notes
Very Important : Methods in .Net treat strings as starting at 0, unlike traditional Delphi where they started at 1.
Microsoft MSDN Links
system
system.String
 
 
A simple example
program Project1;
{$APPTYPE CONSOLE}

var
  strA      : String;
  charArray : Array of Char;
  len       : Integer;

begin
  strA      := 'Hello';
  charArray := strA.ToCharArray;
  len       := Length(charArray);

  Console.WriteLine('strA             = ' + strA);
  Console.WriteLine('charArray        = ' + charArray);
  Console.WriteLine('charArray length = {0}', len.ToString);

  Console.ReadLine;
end.
Show full unit code
  strA             = Hello
  charArray        = Hello
  charArray length = 5
Using only a section of the string
program Project1;
{$APPTYPE CONSOLE}

var
  strA      : String;
  charArray : Array of Char;
  len       : Integer;

begin
  strA      := '0123456789';
  charArray := strA.ToCharArray(6, 2);
  len       := Length(charArray);

  Console.WriteLine('strA             = ' + strA);
  Console.WriteLine('charArray        = ' + charArray);
  Console.WriteLine('charArray length = {0}', len.ToString);

  Console.ReadLine;
end.
Show full unit code
  strA             = 0123456789
  charArray        = 67
  charArray length = 2
 
 
Delphi Programming © Neil Moffatt All rights reserved.  |  Contact the author