STRING
Source: https://www.freebasic.net/wiki/wikka.php?wakka=KeyPgString Last revised: 2024-06-22
Standard data type: 8-bit character string.
Syntax
Dim variable As String [* size]Description
A String is an array of characters.
- Declared without the
sizeparameter, it is dynamically resized (length from 0 bytes to 2 GB). A descriptor contains a pointer to the string, its length, and allocated space.Varptrreturns a pointer to the descriptor;Strptrpoints to the actual string. - Declared with a fixed size (constant or compile-time expression), it is a QB-style fixed-length string. Since fbc 1.20.0, unused characters are set to space (ASCII 32). Before 1.20.0, they were set to 0 and NULL-terminated.
An implicit NULL character (Chr(0)) is added to the end for compatibility with external libraries (ignored by FreeBASIC internally).
Variable names do not require a $ suffix. In -lang fb, suffixes are disallowed.
Examples
Variable length:
vb
Dim a As String
a = "Hello"
Print a
a += ", world!"
Print a
Dim As String b = "Welcome to FreeBASIC"
Print b + "! " + aVariable-length strings as buffers:
vb
Dim As String mybigstring = Space(1024)
Print "buffer address: &h" & Hex( StrPtr( mybigstring ), 8 ) & ", length: " & Len( mybigstring )
mybigstring = "" ' Explicitly destroying
Print "buffer address: &h" & Hex( StrPtr( mybigstring ), 8 ) & ", length: " & Len( mybigstring )Variable-length string as Const parameter:
vb
Sub silly_print( ByRef printme As Const String )
Print ".o0( " & printme & " )0o."
End Sub
Dim As String status = "OK"
silly_print( "Hello FreeBASIC!" )
silly_print( "Status: " + status )Version
- Before fbc 1.20.0, fixed-length strings were NULL-terminated (used
size + 1bytes). - Since fbc 1.20.0, unused characters of fixed-length strings are initialized with space.
Differences from QB
- QB strings limited to 32767 characters.
- In QB, unused characters of fixed-length strings were initialized with space (32).