Difference between revisions of "String"

From Lazarus wiki
Jump to navigationJump to search
m (remove extra blank line)
(21 intermediate revisions by 6 users not shown)
Line 1: Line 1:
 
{{String}}
 
{{String}}
  
'''String''' is a [[Type]] which may contain [[Character_and_string_types#AnsiChar|char]]acters. String is a synonym for [[Character_and_string_types#ShortString|ShortString]] or [[Character_and_string_types#AnsiString|AnsiString]] depending on the current compiler setting.
+
'''String''' is a [[Type|type]] which may contain [[Character_and_string_types#AnsiChar|char]]acters.  
  
If [[Compiler]] directive {$H} or 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.
+
==Usage==
 +
<syntaxhighlight lang="pascal">
 +
var
 +
  s, str1, str2, str3, str4: string;
 +
  c: char;
 +
  n: integer;
  
The ShortString length can be decided by the programmer but is limited to 255 characters. If a ShortString length is not given, the length defaults to 255.
+
str1 := 'abc';    // assignment
 +
str2 := '123';    // string containing chars 1, 2 and 3
 +
str3 := #10#13;    // cr lf
 +
str4 := 'this is a ''quoted'' string';  // use of quotes within a string
 +
s := str1 + str2;  // concatenation
 +
c := s[1];        // use as index in array
 +
n := Length( s );  // length of string s
  
The length of an AnsiString is only limited by available memory.
+
</syntaxhighlight>
  
In the future, there might be a compiler switch to have the String type refer to a Unicode (UTF-8 or UTF-16) String type.
+
==Alias==
 +
String is an alias for [[Character_and_string_types#ShortString|ShortString]], [[Character_and_string_types#AnsiString|AnsiString]] or [[Character_and_string_types#Unicodestring|Unicodestring (UTF16)]] 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} 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]. FPC also supports {$mode delphiunicode} for Delphi compatible UTF16 support.
 +
 
 +
 
 +
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 lang="pascal">
 +
{$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
 +
</syntaxhighlight>
 +
 
 +
A String variable declared with a length specifier will always be a ShortString regardless of the compiler setting for String alias.
 +
 
 +
<syntaxhighlight lang="pascal">
 +
{$H+}            // String is an alias for AnsiString
 +
var
 +
  name : String[25]; // name is a ShortString variable since a length specification overrides the alias setting
 +
</syntaxhighlight>
 +
 
 +
Note that all types of longstring are managed types, whereas ShortStrings are not managed types: they have no reference count.
 +
 
 +
==String types==
 +
The different string types - ShortString, AnsiString, WideString and UnicodeString - differ with respect to ''length'' and ''content'':
 +
*[[Character_and_string_types#ShortString|ShortString]] has a ''fixed maximum 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. It is not reference counted.
 +
*[[Character_and_string_types#AnsiString|AnsiString]] has a ''variable length'' that is limited only by the value of High(SizeInt) (which is platfom dependant) and available memory. It is a reference counted type.
 +
*[[Character_and_string_types#WideString|WideString]] has a variable length like AnsiString but contains [[WideChar]] instead of [[Char]]. It is a BWSTR compatible string type and has no reference count.
 +
*[[Character_and_string_types#UnicodeString|UnicodeString]] is similar to [[Character_and_string_types#WideString|WideString]] but UnicodeString is a managed type and has a reference count whereas widestring is a BWSTR compatible stringtype that is COM compatible and is not reference counted.<br><br>
 +
Note that BWSTR types rely on COM marshaling or - when used alone - copy semantics instead of reference counting. In a COM context they are governed by the COM marshaling subsystem if available. (i.e. Windows)
  
 
== See also ==
 
== See also ==
* [[Character and string types]], a detailled reference covering internal memory layout and access options.
+
* [[Character and string types]], a detailed reference covering internal memory layout and access options.
  
 
{{Data types}}
 
{{Data types}}

Revision as of 16:05, 18 February 2020

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

String is a type which may contain characters.

Usage

var
  s, str1, str2, str3, str4: string;
  c: char;
  n: integer;

str1 := 'abc';     // assignment
str2 := '123';     // string containing chars 1, 2 and 3
str3 := #10#13;    // cr lf
str4 := 'this is a ''quoted'' string';  // use of quotes within a string
s := str1 + str2;  // concatenation
c := s[1];         // use as index in array
n := Length( s );  // length of string s

Alias

String is an alias for ShortString, AnsiString or Unicodestring (UTF16) 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. FPC also supports {$mode delphiunicode} for Delphi compatible UTF16 support.


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

A String variable declared with a length specifier will always be a ShortString regardless of the compiler setting for String alias.

{$H+}            // String is an alias for AnsiString
var
   name : String[25]; // name is a ShortString variable since a length specification overrides the alias setting

Note that all types of longstring are managed types, whereas ShortStrings are not managed types: they have no reference count.

String types

The different string types - ShortString, AnsiString, WideString and UnicodeString - differ with respect to length and content:

  • ShortString has a fixed maximum 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. It is not reference counted.
  • AnsiString has a variable length that is limited only by the value of High(SizeInt) (which is platfom dependant) and available memory. It is a reference counted type.
  • WideString has a variable length like AnsiString but contains WideChar instead of Char. It is a BWSTR compatible string type and has no reference count.
  • UnicodeString is similar to WideString but UnicodeString is a managed type and has a reference count whereas widestring is a BWSTR compatible stringtype that is COM compatible and is not reference counted.

Note that BWSTR types rely on COM marshaling or - when used alone - copy semantics instead of reference counting. In a COM context they are governed by the COM marshaling subsystem if available. (i.e. Windows)

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