Working With Strings
Strings are more than just text containers—they have powerful built-in methods for searching, slicing, and transforming text.
Finding Substrings
The .find() method searches for a substring and returns the index where it first appears, or null if not found.
local chamberName = "Test Chamber 19"
local position = chamberName.find("Chamber")
if (position != null) {
printl("Found 'Chamber' at index: " + position) // Prints: Found 'Chamber' at index: 5
}
// You can start searching from a specific index
local secondSearch = chamberName.find("e", 6) // Start searching after index 6
printl("Next 'e' found at: " + secondSearch) // Finds the 'e' in "Chamber"
Extracting Substrings with Slice
The .slice() method creates a new string from a portion of the original. It takes a start index and an optional end index.
local fullText = "Test Chamber 19"
// Extract "Chamber" (from index 5 to 12, not including 12)
local word = fullText.slice(5, 12)
printl(word) // "Chamber"
// Omit the end index to go to the end of the string
local everythingAfter = fullText.slice(5)
printl(everythingAfter) // "Chamber 19"
// Negative indices count from the END of the string
local lastTwo = fullText.slice(-2)
printl(lastTwo) // "19"
local lastWord = fullText.slice(-9)
printl(lastWord) // "Chamber 19"
Changing Case
Convert strings to uppercase or lowercase with .toupper() and .tolower().
local original = "Test Chamber 19"
printl(original.toupper()) // "TEST CHAMBER 19"
printl(original.tolower()) // "test chamber 19"
// Useful for case-insensitive comparisons
local input = "chell"
if (input.tolower() == "chell") {
printl("Player name matches!")
}
Other Useful String Methods
.len()- Returns the length (number of characters) of the string..tointeger()- Converts the string to an integer. Supports hexadecimal (e.g.,"0xFF")..tofloat()- Converts the string to a floating-point number.
local numString = "42"
local num = numString.tointeger()
printl(num + 8) // 50
local hexString = "0xFF"
printl(hexString.tointeger()) // 255
local length = "Aperture Science".len()
printl(length) // 16