FreeBasic String Keyword
Last modified October 18, 2026
The FreeBasic String keyword represents a sequence of characters
used to store and manipulate text. Strings are fundamental for handling
user input, displaying output, and processing text data.
Basic Definition
In FreeBasic, String is a built-in data type that stores
textual data. Strings can contain letters, numbers, symbols, and spaces.
They are variable-length and can grow or shrink as needed.
String variables are commonly used for input/output operations, text processing, and data storage. FreeBasic provides many built-in functions for string manipulation and conversion.
Declaring String Variables
This example shows how to declare and initialize String variables.
Dim fname As String Dim greeting As String = "Hello, there!" Dim emptyString As String = "" Print "first name: "; fname Print "greeting: "; greeting Print "emptyString: "; emptyString
Here we declare three String variables. The first is uninitialized and defaults to an empty string. The others are explicitly set to a greeting and an empty string. Strings in FreeBasic can hold up to 2GB of text.
String Concatenation
Strings can be combined using the concatenation operator (+).
Dim firstName As String = "John" Dim lastName As String = "Doe" Dim fullName As String = firstName + " " + lastName Print "Full name: "; fullName Dim message As String = "Welcome, " + fullName + "!" Print message
This example demonstrates string concatenation. We combine first and last names with a space between them. Then we create a welcome message by concatenating multiple strings. The + operator efficiently joins strings.
String Length
The Len function returns the number of characters in a string.
Dim text As String = "FreeBasic Programming"
Dim length As Integer = Len(text)
Print "Text: "; text
Print "Length: "; length
Dim emptyLength As Integer = Len("")
Print "Empty string length: "; emptyLength
Here we use the Len function to determine string lengths. The first string contains 21 characters (including the space). The empty string returns 0. Len is useful for validation and processing loops.
String Comparison
Strings can be compared using relational operators.
Dim str1 As String = "apple" Dim str2 As String = "banana" Dim eq As Integer = (str1 = str2) Print str1; " = "; str2; ": "; eq Dim lt As Integer = (str1 < str2) Print str1; " < "; str2; ": "; lt Dim gt As Integer = (str1 > str2) Print str1; " > "; str2; ": "; gt Dim password As String = "secret123" Dim userInput As String = "Secret123" Dim same As Integer = (password = userInput) Print "Password match: "; same
This code compares strings lexicographically (ASCII value order). Relational operators return -1 for true and 0 for false. Note that comparisons are case-sensitive, as shown in the password example. For case-insensitive comparisons, convert both strings with UCase or LCase before comparing.
String Substrings
The Mid function extracts portions of a string.
Dim sentence As String = "The quick brown fox jumps over the lazy dog" Dim word As String = Mid(sentence, 5, 5) Print "Extracted word: "; word Dim rest As String = Mid(sentence, 11) Print "From position 11: "; rest Dim lastThree As String = Mid(sentence, Len(sentence) - 2, 3) Print "Last three characters: "; lastThree
Mid extracts substrings using start position and length. The first call gets "quick". Without length, it returns all characters to the end. We can calculate positions relative to the string length for flexible access.
String Searching
The InStr function finds substrings within strings.
Dim proverb As String = "A stitch in time saves nine." Dim p As Integer = InStr(proverb, "time") Print "'time' found at position: "; p p = InStr(proverb, "zebra") Print "'zebra' found at position: "; p p = InStr(5, proverb, "i") Print "'i' after position 5: "; p
InStr returns the position of the first occurrence of a substring. If not found, it returns 0. We can specify a starting position to search from. This is useful for parsing and text analysis.
String Modification
FreeBasic provides functions to modify string content.
Dim original As String = " FreeBasic is great! "
Dim trimmed As String = Trim(original)
Print "Trimmed: '"; trimmed; "'"
Dim replaced As String = ""
Dim found As Integer = InStr(trimmed, "great")
If found > 0 Then
replaced = Left(trimmed, found - 1) + "awesome" + Mid(trimmed, found + Len("great"))
End If
Print "Replaced: "; replaced
Dim upperCase As String = UCase(replaced)
Print "Uppercase: "; upperCase
Dim lowerCase As String = LCase(replaced)
Print "Lowercase: "; lowerCase
This shows common string modifications: Trim removes leading and trailing spaces, InStr with Left/Mid replaces a substring (FreeBasic has no built-in Replace function), UCase/LCase change case. These functions help clean and standardize string data.
Best Practices
- Initialization: Always initialize strings to avoid null references.
- Memory: Be mindful of memory with very large strings.
- Comparison: Convert with UCase or LCase before comparing if case should be ignored.
- Concatenation: Use & for concatenation when mixing types.
- Security: Validate and sanitize strings from user input.
This tutorial covered the FreeBasic String keyword with practical
examples showing its usage in different scenarios.
Author
List all FreeBasic Tutorials.