BambooBasic provides powerful string manipulation capabilities. Strings are used for text processing, user interaction, and data formatting.
The & operator joins strings together. (For dropping values into a string without hand-joining, see String Interpolation.)
Syntax:
string1 & string2
Examples:
Function Main()
; Join two strings
Local first:String = "Hello"
Local second:String = "World"
Local greeting:String = first & " " & second
Print greeting ; Output: Hello World
; Join multiple strings
Local firstName:String = "Michael"
Local lastName:String = "Denathorn"
Local fullName:String = firstName & " " & lastName
Print "Name: " & fullName
; Mix strings and spaces
Local city:String = "New York"
Local message:String = "Welcome to " & city & "!"
Print message
Return False
EndFunction
Output:
Hello World Name: Michael Denathorn Welcome to New York!
Use ToString() to convert numbers before concatenating:
Function Main()
Local name:String = "Alice"
Local age:Int = 25
Local height:Double = 1.75
; Concatenate string with number
Print name & " is " & ToString(age) & " years old"
Print "Height: " & ToString(height) & " meters"
; Build complex messages
Local score:Int = 9500
Local message:String = "Player " & name & " scored " & ToString(score) & " points!"
Print message
Return False
EndFunction
Output:
Alice is 25 years old Height: 1.75 meters Player Alice scored 9500 points!
Important: You cannot directly concatenate numbers with strings. Always use ToString() for Int and Double values.
String literals are enclosed in double quotes:
Function Main()
; Simple string
Local greeting:String = "Hello!"
; String with spaces
Local phrase:String = "This is a sentence."
; Empty string
Local empty:String = ""
; Strings with special characters
Local path:String = "C:\Users\Documents" ; Backslashes
Local quote:String = "She said, 'Hello'" ; Single quotes inside
Print greeting
Print phrase
Return False
EndFunction
Function Main()
; Start with empty string
Local result:String = ""
; Build it up
result = result & "The "
result = result & "quick "
result = result & "brown "
result = result & "fox"
Print result ; Output: The quick brown fox
Return False
EndFunction
Type TPlayer
Field name:String
Field score:Int
Field level:Int
EndType
Function FormatPlayerInfo:String(player:TPlayer)
Local info:String = ""
info = info & "Player: " & player\name
info = info & " | Level: " & ToString(player\level)
info = info & " | Score: " & ToString(player\score)
Return info
EndFunction
Function Main()
Local p:TPlayer = Create TPlayer
p\name = "Alice"
p\score = 9500
p\level = 12
Local formatted:String = FormatPlayerInfo(p)
Print formatted
Remove p
Return False
EndFunction
Output:
Player: Alice | Level: 12 | Score: 9500
Function GenerateReport:String(title:String, items:Int, total:Double)
Local report:String = ""
; Title
report = report & "=== " & title & " ===" & Chr(10)
; Details
report = report & "Items: " & ToString(items) & Chr(10)
report = report & "Total: $" & ToString(total) & Chr(10)
; Footer
report = report & "===================="
Return report
EndFunction
Function Main()
Local salesReport:String = GenerateReport("Sales Report", 42, 1250.50)
Print salesReport
Return False
EndFunction
Output:
=== Sales Report === Items: 42 Total: $1250.50 ====================
Strings can be compared using standard comparison operators:
Function Main()
Local password:String = "secret123"
Local input:String
Input "Enter password: ", input
If input = password Then
Print "Access granted!"
Else
Print "Access denied!"
EndIf
; Compare strings
Local name1:String = "Alice"
Local name2:String = "Bob"
If name1 = name2 Then
Print "Names match"
Else
Print "Names differ"
EndIf
Return False
EndFunction
Comparison Operators:
Function Main()
Local name:String
Input "Enter your name: ", name
If name = "" Then
Print "You didn't enter a name!"
Else
Print "Hello, " & name & "!"
EndIf
Return False
EndFunction
Function DrawBox(title:String, width:Int)
; Top border
Local border:String = "+"
For i:Int = 1 To width - 2
border = border & "-"
Next
border = border & "+"
Print border
; Title line
Local titleLine:String = "| " & title
; Pad with spaces
Local padding:Int = width - 4 - Len(title)
For i:Int = 1 To padding
titleLine = titleLine & " "
Next
titleLine = titleLine & " |"
Print titleLine
; Bottom border
Print border
EndFunction
Function Main()
DrawBox("Main Menu", 30)
Print ""
Print "1. Start Game"
Print "2. Options"
Print "3. Exit"
Return False
EndFunction
Note: This example assumes a Len() function exists. In base BambooBasic, you may need to calculate string length differently or use fixed widths.
Function FormatCurrency:String(amount:Double)
Return "$" & ToString(amount)
EndFunction
Function FormatPercentage:String(value:Double)
Return ToString(value) & "%"
EndFunction
Function Main()
Local price:Double = 29.99
Local taxRate:Double = 8.5
Local tax:Double = price * (taxRate / 100.0)
Local total:Double = price + tax
Print "=== Receipt ==="
Print "Price: " & FormatCurrency(price)
Print "Tax: " & FormatCurrency(tax) & " (" & FormatPercentage(taxRate) & ")"
Print "Total: " & FormatCurrency(total)
Return False
EndFunction
Output:
=== Receipt === Price: $29.99 Tax: $2.54915 (8.5%) Total: $32.53915
Function IsValidEmail:Int(email:String)
; Simple check: must contain @
; In real code, would be more thorough
Local hasAt:Int = 0
; Check if @ symbol exists (this is simplified)
; In practice, you'd need string search functions
If email <> "" Then
hasAt = 1 ; Simplified for example
EndIf
Return hasAt
EndFunction
Function Main()
Local email:String
Local valid:Int = 0
While valid = 0
Input "Enter your email: ", email
If email = "" Then
Print "Email cannot be empty!"
Else
Print "Email entered: " & email
valid = 1
EndIf
Wend
Return False
EndFunction
While BambooBasic doesn't have built-in substring functions, you can process strings character by character using Chr() and Asc():
Function Main()
Local text:String = "Hello"
; To process each character, you would typically use
; string manipulation functions from a runtime library
Print "Text: " & text
; Example: Check first character
Local firstChar:Int = Asc(text)
Print "First character code: " & ToString(firstChar)
Print "First character: " & Chr(firstChar)
Return False
EndFunction
| Operation | Syntax | Example |
|---|---|---|
| Concatenation | str1 & str2 | "Hello" & " " & "World" |
| Comparison | str1 = str2 | If name = "Alice" |
| Empty check | str = "" | If input = "" |
| Build incrementally | str = str & "text" | result = result & "more" |
| Add newline | str & Chr(10) | text & Chr(10) & "Next line" |
See the String Concatenation Example for demonstrations.
BambooBasic © 2026 Michael Denathorn