You are here

Different String Types in Delphi

Submitted by Asif Nowaj, Last Modified on 2019-11-22

In Delphi, String is a sequence of characters. There different types of string available in Delphi for different purposes.
Types of string are:
1. ShortString
2. AnsiString
3. WideString
ShortString
Maximum length can be 255 characters. Memory required for this type of string is 2 to 256 bytes. Generally, this is used for backward compatibility.
AnsiString

This type of string is being used mostly. Sometimes, it is called long string. When we say string in Delphi, it means AnsiString. It takes maximum length of ~2^31 characters. Storage required for this type of string is 4 bytes to 2 GB. It represents 8 bit ANSI characters, DBCS ANSI, MBCS ANSI etc.

In Delphi, when we declare a variable as string, it represents ANSI String. In default {H+} state, string represents as ANSI string. To change default behaviour for string to short string, we have to use {H-} directive and any declaration after this directive will be considered as short string.

WideString

This type of string can hold Unicode characters which are being used for serving different languages in different locales. Maximum length can be ~2^30 characters. Memory required for this type of string is 4 byte to 2 GB. Some technology like XML Wide String is used as native type. If an application is targeted for different locales then WideString needs to be used throughout the application. While you are using WideString, you should remember one thing that, all the components/controls/third party controls that are being used in your application, should be compatible with WideString.

Most of strings manipulating overloaded functions are available for both string and WideString.
Note: In RAD Studio, string is alias for UnicodeString (UnicodeString and WideString). String in Delphi uses pointer and follow reference counting for memory allocation.

Discussion or Comment

If you have anything in mind to share, please bring it in the discussion forum here.

https://forum.everyething.com/delphi-f39/