Reading — step 1 of 5
Learn
~1 min readArrays and Strings
BASIC has classic string functions, mostly with $ suffix in older dialects (FreeBASIC accepts both).
DIM s AS STRING = "hello world"
LEN(s) ' 11
UCASE(s) ' HELLO WORLD
LCASE("HI") ' hi
MID(s, 7, 5) ' "world" — substring (start, length)
LEFT(s, 5) ' "hello"
RIGHT(s, 5) ' "world"
INSTR(s, "world") ' 7 — position (1-indexed) or 0 if not found
INSTR(s, "xyz") ' 0
STR(42) ' " 42" (note leading space for sign)
VAL("42") ' 42 — parse to number
LTRIM(" hi") ' "hi" — remove leading whitespace
RTRIM("hi ") ' "hi"
TRIM(" hi ") ' "hi"
Concatenation — both + and & work:
DIM greeting AS STRING = "Hello, " + name + "!"
Replacing isn't built into all BASIC dialects. FreeBASIC has Replace:
#include "string.bi" ' if needed
' or just write your own using INSTR + LEFT + RIGHT
Split / parse — older BASIC has no built-in split. Common pattern:
Function CountWords(s AS STRING) AS INTEGER
DIM count AS INTEGER = 0
DIM i AS INTEGER
DIM in_word AS INTEGER = 0
FOR i = 1 TO LEN(s)
IF MID(s, i, 1) = " " THEN
in_word = 0
ELSE
IF in_word = 0 THEN count += 1
in_word = 1
END IF
NEXT i
Return count
End Function
Discussion
Ask a question, share an insight, or help someone who’s stuck.
Sign in to post a comment or reply.
Loading…