FreeBasic Strings
Last modified October 18, 2026
Text is handled with the String type. This article covers joining strings,
measuring and extracting parts of them, searching, converting between text and numbers, comparing
values, and fixed-length strings. For the type itself, see the
String keyword page; the
ZString and
WString pages cover the other string types.
Joining and measuring
Strings are joined with the & operator, and FreeBasic supplies functions for
common operations such as measuring length and changing case. The example builds one string from
two words and then inspects the result.
Dim first As String = "Free" Dim second As String = "BASIC" Dim joined As String = first & second Print joined Print "Length: "; Len(joined) Print "Upper: "; UCase(joined) Print "Lower: "; LCase(joined)
The & operator joins two strings into a new one. Len returns the
number of characters, which is 9 for FreeBASIC. UCase and
LCase return copies in upper and lower case; the original string is unchanged.
$ fbc -lang fb str_basic.bas $ ./str_basic FreeBASIC Length: 9 Upper: FREEBASIC Lower: freebasic
Extracting and searching
Left and Right take characters from the ends of a string.
Mid takes characters starting at a position. Positions start at 1. InStr
returns the position of the first match, or 0 when there is no match. Trim removes
leading and trailing spaces, and String(n, char) builds a string of repeated
characters.
Dim s As String = "FreeBASIC programming" Print "Left 4: "; Left(s, 4) Print "Right 11: "; Right(s, 11) Print "Mid from 9, 5 chars: "; Mid(s, 9, 5) Print "Position of 'pro': "; InStr(s, "pro") Print "Not found: "; InStr(s, "Java") Dim padded As String = " spaced " Print "Trimmed: ["; Trim(padded); "]" Print "Repeated: "; String(5, "*")
Left(s, 4) returns the first four characters. Right(s, 11) returns the last
eleven. Mid(s, 9, 5) starts at position 9, the letter C, and takes five characters.
InStr(s, "pro") finds the text at position 11. A search for Java returns 0.
$ fbc -lang fb str_functions.bas $ ./str_functions Left 4: Free Right 11: programming Mid from 9, 5 chars: C pro Position of 'pro': 11 Not found: 0 Trimmed: [spaced] Repeated: *****
Converting between text and numbers
Str converts a number to a string, and Val converts a string to a
number. Chr gives the character for a code, and Asc gives the code of a
character.
Dim n As Integer = 1234
Dim text As String = Str(n)
Print "Text length: "; Len(text)
Dim value As Double = Val("3.75") * 2
Print "Value doubled: "; value
Print "Character 65: "; Chr(65)
Print "Code of 'a': "; Asc("a")
Str(1234) produces the four-character text 1234, so its length is 4.
Val("3.75") reads the number 3.75, which is then doubled. Chr(65) is the
letter A and Asc("a") is 97.
$ fbc -lang fb str_convert.bas $ ./str_convert Text length: 4 Value doubled: 7.5 Character 65: A Code of 'a': 97
Comparing strings
FreeBasic compares strings character by character with the relational operators =,
<>, <, >, <=, and
>=. The comparison is case-sensitive by default, and a comparison used as a
number is true (-1) or false (0). The example compares two words and then repeats the equality
test with both sides converted to upper case.
Dim a As String = "apple" Dim b As String = "Apple" Dim eq As Integer = (a = b) Dim lt As Integer = (a < "banana") Print "a = b (case-sensitive): "; eq Print "a < banana: "; lt Print "Case-insensitive equal: "; (UCase(a) = UCase(b))
String comparison is case-sensitive: apple and Apple differ, so
the first result is 0. The relational operator < compares strings alphabetically,
and apple comes before banana, which gives -1. A comparison that is true
prints as -1. To ignore case, convert both strings with UCase before comparing.
$ fbc -lang fb str_compare.bas $ ./str_compare a = b (case-sensitive): 0 a < banana: -1 Case-insensitive equal: -1
Fixed-length strings
A String * n variable always holds exactly n characters. Shorter text
is padded with NUL characters (character code 0), and longer text is truncated.
Dim code As String * 6 = "AB" Print "Length: "; Len(code) Print "Trimmed: ["; Trim(code); "]" Print "Char 3 code: "; Asc(Mid(code, 3, 1)) Dim wide As String * 6 = "ABCDEFGHIJ" Print "["; wide; "]"
code holds six characters, so "AB" is padded with four NUL characters.
Trim removes them for display, and the third character has code 0. The ten-letter
text is cut to six characters. Fixed-length strings are useful for records stored in files,
because every record has the same size.
$ fbc -lang fb str_fixed.bas $ ./str_fixed Length: 6 Trimmed: [AB] Char 3 code: 0 [ABCDEF]
Source
Author
List all FreeBasic Tutorials.