DelphiBasics
WideString
Type
A data type that holds a string of WideChars System unit
type WideString;
Description
The WideString data type is used to hold sequences of International characters, like sentences.
 
Each character is a WideChar, guaranteed to be 16 bits in size.
 
WideChar types provide support for multi-byte International character sets such as Chinese, which have vast numbers of characters (idiograms) in their character sets.
 
Unlike ShortStrings, WideStrings are pointer referenced variables (for Windows - for Linux, they are not). Storage is allocated for an AnsiString only when needed. For example, assigning the value of one AnsiString to another does not allocate storage for a copy of the first string. Instead, the reference count of the first string is incremented, and the second AnsiString set to point to it.
 
But when the second string is changed, new storage is obtained for this new string, and the reference count for the first string is decremented.
 
When a string is no longer referenced (the last AnsiString referer is set to nil), it is discarded. This is an example of Delphi managing storage on your behalf.
 
WideStrings can be assigned from other strings, from functions that return a string, and with concatenations as in the sample code.
 
When assigning to a 'narrow' string, such as an AnsiString, the double to single byte conversion can have unpredictable results. Use with care.
 
Converting from AnsiString to WideString is trouble free.
Notes
Strings are indexed with 1 for the first character (arrays start with 0 for the first element).
Related commands
AnsiCompareStrCompare two strings for equality
AnsiLowerCaseChange upper case characters in a string to lower case
AnsiPosFind the position of one string in another
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
PWideStringPointer to a WideString value
SetLengthChanges the size of a string, or the size(s) of an array
StringA data type that holds a string of characters
WideCharVariable type holding a single International character
 Download this web site as a Windows program.




 
Example code : Assign to two WideStrings and manipulate these
var
  string1, string2 : WideString;
begin
  // Assign a famous sentence to the first string
  string1 := 'Hello World';

  // Assign to the second string
  // This simply points string2 at string1
  // The 'Hello World' string storage has a reference count of 2
  string2 := string1;

  // Add to the second string
  // This disassociates from string1 - new string storage is
  // created to hold the string2 value
  string2 := string2 + ', how is everyone?';

  // And finally, set the length of the first string to 5
  SetLength(string1, 5);

  // Display both strings
  ShowMessage('String1 = '+string1);
  ShowMessage('String2 = '+string2);
end;
Show full unit code
  String1 = Hello
  String2 = Hello World, how is everyone?
 
Delphi Programming © Neil Moffatt . All rights reserved.  |  Home Page