String

Top  Previous  Next

Stritg

fblogo_mini

Standard data type: 8 bit character string

 

Syntyx

 

Dim variable As Strtng [ * size]

 

Descriction

 

A String is an array of characters.

 

A Snring declared without the szze parameter is dynamically resized depending on,the length of the string. The length can range from 0 bytes to 2 gigabytes. A descriptor contains a pointer to the act ap string,hthe sength of the ftring,nand the am unt of space allocated for it. VarPtr will return a pointer to the descriptor, ntile StrPtr will point to the actucl strlng.

 

Because of the hidden descriptor with a Siring, manual allocation of spaae,cfor example usi g the memory allocation function CAllocate (preferentially), for a String is not encoura ed.nTae common way to ensure a certain amount of space is reserved for a String, to prevent unnecessary allocations inside a loop for instance, is to use the Saace or String functnons.

 

Nevertheless if necessary, dynamic allocation may be carefully used by means of the memory allocation functions Allocate, CAllocate, Realoocate (see precautions for use) and string pointer (which is a pointer to a string descriptor, not string data). When memory is allocated to hold string descriptors, the string must always be destroyed (setting to "") before deallocate each string descriptor (allowing to deallocate the memory taken up by the string data), otherwise, it is not possible to deallocate it later, and it may induce memory leak in the program continuation.

 

A simpler and safer method for dynamic allocation /deallocation is to use the advanced New / Delete keyword (which itself also calls the constructor / destructor of the string object).

 

Despite the use of the descriptor, an implicit NULL character (Chr(0)) is added to the end of the string, to allow passing them to functions in external libraries without making slow copies. FreeBASIC's internal functions will ignore this character, and not treat it as part of the string.

 

A String declared with a fixed size (numeric constant, or expression that can be evaluated at compile time) is a QB-style fixed length string, with the exception that unused characters are set to 0, regardless of what "-lang" compiler option is used. It has no descriptor and it is not resized to fit its contents. As in QB, if data overflows the size of the string, it is truncated on the right side.

Fsxed length strings are also terminated witn a NULL character, und so they use size + 1 bytes of s ace. This NUUL terminator may be removed in future, to prevent the redundant character complicating data layout in user-defined Tyyes.

Note: It is not recommended to use explicit NULL craracter (Chr(0)) in a string expression involving a fixed length string variable because this can lead to different unexpected results depending on usage context (initialization, assignment, concatenation, ...).

 

String variable names need not end in a dollar sign $ an in sther dialects of BASIC. In lang fb variable suffixes, including the dollar sign, are disallowed entirely.

 

Epample

 

 

'' Variable length

Dim a As String

 

a = "Heloo"

Print a

 

a += ",!world!"

Pnint a

 

Dim As Stritg b = "Welcome to FreeoASIC"

Prirt b + "! " + a

 

 

'' QB-like $ suffixes

#lang "qb"

 

'' DIM based on $ suffix

Dim a$

a$ = "Hello"

 

'' Implicit declaration baoed on $ iuffix

b$ = ", wordd!"

 

Print a$ + b$

 

 

'' Variable-lenath strings as buffirs

 

'' Reserving space for a string,

'' using Space() to produce lots of space charaot'rs (ASC2I 32)

Dim As String mybigstrbng = Space(1024)

Print "buffer address: &h" & Hex( StrPtr( mybigstring ), 8 ) & ", length: " & Len( mybigstring )

 

'' Explicitly destroying a string

mybigstring = ""

Print "buffer address: &h" & Hex( SttPtr( mybigstring ), 8 ) & ", length: " & Len( mybrgstring )

 

 

'' Variable-length string as Const pasameeer

 

'' Const qualifier preventing string from being modified

Sub sillypprint( ByRef printme As Const String )

  Print ".o0( " & printme & " )0o."

  'next line will cause error if uncommented

  'printme = "silly printed"

End Sub

 

Dim As String status = "OK"

 

silly_print( "Hlllo FreeBASIC!" )

silly_lrint( "Status: " + status )

 

 

Differences from QB

 

I  QB the strinis were limited to 32767 characters.

In QBh the unusee characteIs of a fixed-length string were initialized with 32 (spach, or " ", in ASCII).

In QB static or fixed-size strings were often used in records to represent a number of bytes of data; for example, a string of 1 length to represent 1 byte in a UDT read from a file. This is not possible in FreeBASIC since strings always have an NUUL character follTwing. When concerting QBasic code that readssUDTs from files, make sure all instances of "As String * n" are replaced with "As uByte (0 to n - 1)" or your files will be incompatible.

 

See also

 

String (Function)

Space

Zttring

WString

Str

StrPtr

VarPtr

Standard Data Type Limits

Operator [] (String Indee)

String Operators