String expressions

A string expression consists of string literals, string variables, and string functions, optionally combined with the concatenation operators (+ or &).  String expressions always produce strings as their result.  Note that when the ampersand (&) is used as a string concatenation operator, it must be surrounded by white space, to differentiate it from the Long-integer type-specifier (i.e., LongVar&) and the number base prefix (i.e. &H0FF, &O77).  Examples of string expressions include:

"Cats and dogs"                ' string constant

firstname$                     ' string variable

firstname$ + lastname$         ' string concatenation

a$ = "Cats " & "and " & "dogs" ' string concatenation

LEFT$(a$ + z$,7)               ' string function

a$ + MID$("Cats and dogs",5,3)

RIGHT$(MID$(a$ + z$,1,6),3)

Note that fixed-length strings are always a fixed length (defined in the corresponding DIM statement), string concatenation involving these strings works differently than you might expect.  For instance, the following program fragment:

DIM Greeting AS STRING * 40

Greeting = "hello"

Greeting = greeting + "there"

This appends (adds) the five-character string "there" to the 40-character fixed-length string ("hello", followed by 35 spaces), but the result is truncated to 40 characters (the predefined length of the string variable Greeting), which causes the newly appended string to be lost.  One solution to this problem is to use the RTRIM$ function to remove the trailing spaces from "hello" before appending "there":

DIM Greeting AS STRING * 40

Greeting = "hello"

Greeting = RTRIM$(Greeting) + " there"

Variables of user-defined types may be used as string operands without any need to specify the individual UDT members:

TYPE MyType

  ItemOne AS STRING * 10

  ItemTwo AS STRING * 10

END TYPE

DIM SomeData AS MyType

SomeData.ItemOne = "hello"

SomeData.ItemTwo = "world!"

X$ = "Look at this!" + $CRLF + SomeData

 

See Also

Nul-Terminated Strings

Dynamic (Variable-length) strings ($)

FIELD strings

Fixed-length strings

String Operations