Strings & String Functions

Strings are compared by character codes ("B" > "A", "a" > "Z"); compare case-insensitively with UCASE$/LCASE$.

15.1 Basics

Strings are compared by character codes ("B" > "A", "a" > "Z"); compare case-insensitively with UCASE$/LCASE$.

a$ = "Hello"
b$ = "World"
c$ = a$ + ", " + b$ + "!"         ' concatenation with +
PRINT c$, LEN(c$)                 ' Hello, World!   13
PRINT CHR$(34); "quoted"; CHR$(34) ' "quoted"
empty$ = ""
IF empty$ = "" THEN PRINT "empty"

15.2 Function reference

Example → result LEN("BASIC") → 5 LEFT$(s$, n) first n characters LEFT$("BASIC", 3) → "BAS" RIGHT$(s$, n) last n characters RIGHT$("BASIC", 2) → "IC" MID$(s$, start[, n]) MID$("BASIC", 2, 3) → "ASI" INSTR([start,] s$, find$) position of find$ or 0 INSTR("BASIC", "SI") → 3 _INSTRREV(s$, find$) last position (QB64) _INSTRREV("a.b.c", ".") → 4 UCASE$(s$) / LCASE$(s$) upper / lower case UCASE$("qb64") → "QB64" LTRIM$ / RTRIM$ / _TRIM$ remove leading / trailing / both spaces _TRIM$(" hi ") → "hi" number as string (leading space if ≥ 0) STR$(42) → " 42" number from string

VAL("12.5") → 12.5; VAL("&HFF") → 255 character for ASCII code CHR$(65) → "A" ASC(s$[, pos]) code of a character ASC("A") → 65 SPACE$(3) → " " STRING$(n, char) n copies of a character STRING$(5, "*") → "*****" HEX$(n) / OCT$(n) hexadecimal / octal text HEX$(255) → "FF" INPUT$(n[, #f]) n characters from keyboard or file DATE$ / TIME$ "mm-dd-yyyy" / "hh:mm:ss"

15.3 MID$ as a statement

MID$ can also replace characters in place (the length never changes):

s$ = "Hello World"
MID$(s$, 7, 5) = "BASIC"
PRINT s$                  ' Hello BASIC

15.4 Common string algorithms

PRINT Reverse$("stressed")                         ' desserts
PRINT IsPalindrome%("A man, a plan, a canal: Panama")
PRINT CountWords%("  the quick   brown fox ")       ' 4
PRINT ReplaceAll$("a-b-c", "-", " + ")              ' a + b + c
PRINT Caesar$("Hello", 3)                           ' Khoor
END

FUNCTION Reverse$ (s AS STRING)
    DIM r AS STRING, i AS LONG
    FOR i = LEN(s) TO 1 STEP -1
        r = r + MID$(s, i, 1)
    NEXT
    Reverse$ = r
END FUNCTION

FUNCTION IsPalindrome% (s AS STRING)
    DIM t AS STRING, ch AS STRING, i AS LONG
    FOR i = 1 TO LEN(s)                      ' keep letters and digits only
        ch = UCASE$(MID$(s, i, 1))
        IF (ch >= "A" AND ch <= "Z") OR (ch >= "0" AND ch <= "9") THEN t = t + ch
    NEXT
    IsPalindrome% = (t = Reverse$(t))
END FUNCTION

FUNCTION CountWords% (s AS STRING)
    DIM i AS LONG, inWord AS INTEGER, n AS INTEGER
    FOR i = 1 TO LEN(s)
        IF MID$(s, i, 1) <> " " THEN
            IF NOT inWord THEN n = n + 1: inWord = -1
        ELSE
            inWord = 0
        END IF
    NEXT
    CountWords% = n
END FUNCTION

FUNCTION ReplaceAll$ (s AS STRING, find AS STRING, repl AS STRING)
    DIM result AS STRING, p AS LONG,
…

15.5 Number formatting helpers

n = 7
PRINT RIGHT$("000" + _TRIM$(STR$(n)), 3)     ' 007  (zero padding)
PRINT LTRIM$(STR$(3.5)) + " kg"              ' 3.5 kg
PRINT RIGHT$(SPACE$(10) + "right", 10)       ' right-aligned in 10 columns
PRINT HEX$(48879)                            ' BEEF

15.6 ASCII and character codes

Codes 0–31 are control characters (CHR$(13) carriage return, CHR$(10) line feed, CHR$(9) tab, CHR$(27) escape), 32–126 are printable ASCII, and 128–255 come from the active code page (code page 437 on DOS: box-drawing characters such as CHR$(201) ╔, CHR$(205) ═, CHR$(219) █). QB64-PE renders code page 437 by default in its text screens.

Classic BASIC: Microsoft BASICs had LEFT$, RIGHT$, MID$, LEN, STR$, VAL, CHR$, ASC; Sinclair BASIC used slicing instead (A$(2 TO 4)); many 8-bit BASICs limited strings to 255 characters. FreeBASIC: same functions plus Trim, InStrRev, Format (via vbcompat.bi), ZString and WString (Unicode); string indexing s[0] gives a byte value. VB.NET: strings are immutable objects: s.Length, s.Substring(1, 3), s.IndexOf("x") (zero-based), s.ToUpper(), s.Trim(), s.Split(","c), s.Replace("a", "b"), String.Join, StringBuilder for heavy concatenation; legacy Left, Mid, Len exist in Microsoft.VisualBasic.