Reading — step 1 of 5
Learn
FreeBASIC string functions are inherited from QuickBASIC + a few modern additions.
String functions
DIM s AS STRING
s = "hello world"
LEN(s) ' 11
UCASE(s) ' "HELLO WORLD"
LCASE(s) ' "hello world"
LEFT(s, 5) ' "hello"
RIGHT(s, 5) ' "world"
MID(s, 7, 5) ' "world" (start, length — 1-indexed!)
MID(s, 7) ' "world" (to end)
LTRIM(s) ' strip leading whitespace
RTRIM(s) ' strip trailing whitespace
TRIM(s) ' both
INSTR(s, "world") ' 7 (1-indexed position)
INSTR(8, s, "l") ' 10 (search starting from 8)
INSTR(s, "xyz") ' 0 (not found)
Note: positions are 1-indexed in BASIC string functions — historic legacy.
Concatenation
DIM full AS STRING
full = "Hello, " & "World!"
' or:
full = "Hello, " + "World!" ' also works for strings
Use & for clarity — + is ambiguous (could mean numeric add).
Character / number conversion
ASC("A") ' 65 (ASCII code)
CHR(65) ' "A"
CHR(13) ' carriage return
STR(42) ' "42" (no leading space in FreeBASIC)
VAL("42 hello") ' 42 (parse leading number)
VAL("abc") ' 0 (no number found)
Old QBASIC's STR$ put a leading space before positive numbers; FreeBASIC's STR does not — STR(42) is exactly "42", and negatives keep their - sign. (The $-suffixed names like STR$/CHR$ are QB-dialect only and will not compile here.)
PRINT formatting
Comma — column zones (every 14 chars):
PRINT "a", "b", "c"
' a b c
Semicolon — no spacing (concat):
PRINT "a";"b";"c"
' abc
Mixing:
DIM person AS STRING = "Ada"
DIM age AS INTEGER = 36
PRINT person; " is "; age; " years old"
' Ada is 36 years old
Note: FreeBASIC's PRINT does NOT add spaces around numbers joined with ; (old QBASIC did) — put the spaces in your strings, as " is " and " years old" do above.
PRINT USING — formatted output
PRINT USING "###.##"; 3.14159 ' " 3.14"
PRINT USING "$$#,##0.00"; 1234.5 ' "$1,234.50"
PRINT USING "\ \"; "hi" ' left-pad to backslash gap
The template chars:
#— digit position0— required digit (zero pad).— decimal point,— thousands separator$— currency\...\— string field of N chars+/-— sign
Closest BASIC has to printf.
sprintf-style with FORMAT
FreeBASIC has a FORMAT function (put #include "string.bi" at the top of your file):
#include "string.bi"
DIM s AS STRING
s = FORMAT(3.14159, "0.00") ' "3.14"
s = FORMAT(99.5, "0.0\%") ' "99.5%"
For more complex formatting, build with & and helper functions.
Multi-line strings
No triple-quote literals. Use CHR(10) for newlines:
DIM block AS STRING
block = "line 1" & CHR(10) & "line 2" & CHR(10) & "line 3"
PRINT block
Discussion
Ask a question, share an insight, or help someone who’s stuck.
Sign in to post a comment or reply.
Loading…