DelphiBasics
String
Type
A data type that holds a string of characters System unit
type String;
1
2 type String[FixedSize constant];
Description
The String data type is used to hold sequences of characters, like sentences.
 
String is by default, actually treated as a AnsiString. It can be treated as a ShortString if the $LongStrings compiler directive is set Off (the default is On).
 
An AnsiChar can hold any number of characters, restricted only by memory.
 
Version 2 however, forces the string to be a ShortString by defining a FixedSize (up to 255 characters) of the string. This is particularly important when creating many strings, and especially so when storing strings in records (as in example 2).
 
Strings can be assigned from other strings, from functions that return a string, and with concatenations as in the sample code.
Notes
Strings are indexed with 1 for the first character (arrays start with 0 for the first element).
Related commands
$LongStringsTreat string types as AnsiString or ShortString
AnsiCompareStrCompare two strings for equality
AnsiLowerCaseChange upper case characters in a string to lower case
AnsiPosFind the position of one string in another
AnsiStringA data type that holds a string of AnsiChars
AnsiUpperCaseChange lower case characters in a string to upper case
ConcatConcatenates one or more strings into one string
CopyCreate a copy of part of a string or an array
DeleteDelete a section of characters from a string
LengthReturn the number of elements in an array or string
MoveCopy bytes of data from a source to a destination
PStringPointer to a String value
SetLengthChanges the size of a string, or the size(s) of an array
ShortStringDefines a string of up to 255 characters
WideStringA data type that holds a string of WideChars
 Download this web site as a Windows program.




 
Example code : Assigning to a string and then adding a bit more
var
  myString : String;
begin
  // Assign a famous sentence to this string
  myString := 'Hello World';

  // Add to this string
  myString := myString + ', how is everyone?';

  // Display the final myString value
  ShowMessage('myString = '+myString);
end;
Show full unit code
  Hello World, how is everyone?
 
Example code : Using fixed length strings in a record
type
  // Declare a customer record
  TCustomer = Record
    firstName : String[15];
    lastName  : String[30];
  end;

var
  customer : TCustomer;

begin
  // Set up the John's customer details
  with customer do
  begin
    firstName := 'John';
    lastName  := 'Smith';
  end;

  // Now show the details of our customer
  ShowMessage('Customer name = '+customer.firstName+
                             ' '+customer.lastName);
end;
Show full unit code
  Customer name = John Smith
 
Delphi Programming © Neil Moffatt 2002 - 2022. All rights reserved.  |  Home Page