ZetCode

FreeBasic String Keyword

last modified June 16, 2025

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.

string_declare.bas
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 (+).

string_concat.bas
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.

string_length.bas
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 20 characters (including space). The empty string returns 0. Len is useful for validation and processing loops.

String Comparison

Strings can be compared using relational operators.

string_compare.bas
Dim str1 As String = "apple"
Dim str2 As String = "banana"

Print str1; " = "; str2; ": "; (str1 = str2)
Print str1; " < "; str2; ": "; (str1 < str2)
Print str1; " > "; str2; ": "; (str1 > str2)

Dim password As String = "secret123"
Dim input As String = "Secret123"
Print "Password match: "; (password = input)

This code compares strings lexicographically (ASCII value order). Note that comparisons are case-sensitive, as shown in the password example. FreeBasic also provides StrComp for case-insensitive comparisons.

String Substrings

The Mid function extracts portions of a string.

string_substring.bas
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.

string_search.bas
Dim proverb As String = "A stitch in time saves nine."

Dim pos As Integer = InStr(proverb, "time")
Print "'time' found at position: "; pos

pos = InStr(proverb, "zebra")
Print "'zebra' found at position: "; pos

pos = InStr(5, proverb, "i")
Print "'i' after position 5: "; pos

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.

string_modify.bas
Dim original As String = "   FreeBasic is great!   "

Dim trimmed As String = Trim(original)
Print "Trimmed: '"; trimmed; "'"

Dim replaced As String = Replace(trimmed, "great", "awesome")
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 whitespace, Replace changes substrings, UCase/LCase change case. These functions help clean and standardize string data.

Best Practices

This tutorial covered the FreeBasic String keyword with practical examples showing its usage in different scenarios.

Author

My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.

List all FreeBasic Tutorials.