Difference between revisions of "String"

From Lazarus wiki
Jump to navigationJump to search
(Add back line inadvertently deleted)
(reword)
Line 4: Line 4:
  
 
==Alias==
 
==Alias==
String is an alias for [[Character_and_string_types#ShortString|ShortString]] or [[Character_and_string_types#AnsiString|AnsiString]] depending on the current compiler setting.
+
String is an alias for [[Character_and_string_types#ShortString|ShortString]] or [[Character_and_string_types#AnsiString|AnsiString]] depending on a compiler setting.
  
If [[Compiler|compiler]] directive {$H} or [http://freepascal.org/docs-html/current/prog/progch1.html#x5-40001 compiler directive] {$LongStrings} is on ( {$H+}  or {$LongStrings ON} ), then a String type is the same as an AnsiString type, if not ( {$H-} or {$LongStrings OFF} ), it is a ShortString type. What String is an alias for can also be set by the -Sh [http://www.freepascal.org/docs-html/user/userap1.html command line option].  
+
If [[Compiler|compiler]] directive {$H} or [http://freepascal.org/docs-html/current/prog/progch1.html#x5-40001 compiler directive] {$LongStrings} has been used with an "on" parameter ( {$H+}  or {$LongStrings ON} ), then a String type is the same as an AnsiString type, if not ( {$H-} or {$LongStrings OFF} ), it is a ShortString type. What String is an alias for can also be set by the -Sh [http://www.freepascal.org/docs-html/user/userap1.html command line option].  
  
NOTE: The {$mode} compiler directive has an effect on the current setting for which type a String is an alias to. After the compiler mode is set to FPC, ObjFPC, MacPAS or TP, String will be an alias for ShortString. After the compiler mode is set to Delphi, String will be an alias for AnsiString. You can override the String setting of a particular mode by following the {$mode} directive with a compiler directive to set the String alias:
+
NOTE: The {$mode} compiler directive will also set the String alias. After the compiler mode is set to FPC (the default), ObjFPC, MacPAS or TP, String will be an alias for ShortString. After the compiler mode is set to Delphi, String will be an alias for AnsiString. So the String alias setting should be made following the compiler mode setting to prevent it from being overridden:  
  
 
<syntaxhighlight>
 
<syntaxhighlight>
{$mode ObjFPC}  // String is an alias for ShortString
+
{$H+}            // String is an alias for AnsiString
 +
{$mode ObjFPC}  // also affects String alias - String is now an alias for ShortString
 
{$H+}            // String is now an alias for AnsiString
 
{$H+}            // String is now an alias for AnsiString
 
</syntaxhighlight>
 
</syntaxhighlight>
  
In the future there may be a compiler directive to set the String type to refer to a Unicode string (UTF-8 or UTF-16) type.  
+
In the future there may be a compiler directive to set String to refer to a Unicode string type (UTF-8 or UTF-16).  
  
 
==ShortString/AnsiString==
 
==ShortString/AnsiString==
The potential alias types for type String - ShortString and AnsiString - differ with respect to ''length'':
+
The potential alias types for String - ShortString and AnsiString - differ with respect to ''length'':
  
 
*ShortString has a ''fixed length'' that is decided by the programmer (e.g. ''name : String[25];'') but is limited to 255 characters. If a ShortString length is not explicitly given, then the length is implicitly  set to 255.  
 
*ShortString has a ''fixed length'' that is decided by the programmer (e.g. ''name : String[25];'') but is limited to 255 characters. If a ShortString length is not explicitly given, then the length is implicitly  set to 255.  
  
 
*AnsiString has a ''variable length'' that is limited only by the value of High(SizeInt) (which is platfom dependant) and available memory.
 
*AnsiString has a ''variable length'' that is limited only by the value of High(SizeInt) (which is platfom dependant) and available memory.
 
 
 
  
 
== See also ==
 
== See also ==

Revision as of 13:35, 10 July 2016

Deutsch (de) English (en) español (es) français (fr) русский (ru)

String is a type which may contain characters.

Alias

String is an alias for ShortString or AnsiString depending on a compiler setting.

If compiler directive {$H} or compiler directive {$LongStrings} has been used with an "on" parameter ( {$H+} or {$LongStrings ON} ), then a String type is the same as an AnsiString type, if not ( {$H-} or {$LongStrings OFF} ), it is a ShortString type. What String is an alias for can also be set by the -Sh command line option.

NOTE: The {$mode} compiler directive will also set the String alias. After the compiler mode is set to FPC (the default), ObjFPC, MacPAS or TP, String will be an alias for ShortString. After the compiler mode is set to Delphi, String will be an alias for AnsiString. So the String alias setting should be made following the compiler mode setting to prevent it from being overridden:

{$H+}            // String is an alias for AnsiString
{$mode ObjFPC}   // also affects String alias - String is now an alias for ShortString
{$H+}            // String is now an alias for AnsiString

In the future there may be a compiler directive to set String to refer to a Unicode string type (UTF-8 or UTF-16).

ShortString/AnsiString

The potential alias types for String - ShortString and AnsiString - differ with respect to length:

  • ShortString has a fixed length that is decided by the programmer (e.g. name : String[25];) but is limited to 255 characters. If a ShortString length is not explicitly given, then the length is implicitly set to 255.
  • AnsiString has a variable length that is limited only by the value of High(SizeInt) (which is platfom dependant) and available memory.

See also


navigation bar: data types
simple data types

boolean byte cardinal char currency double dword extended int8 int16 int32 int64 integer longint real shortint single smallint pointer qword word

complex data types

array class object record set string shortstring