Strings
The Strings module provides a comprehensive set of string manipulation and processing functions for FiveM resource development.
split
Splits a string into an array of substrings using a delimiter.
B2Lib.Strings.split(str, delimiter)
Parameters:
str
(string): The string to splitdelimiter
(string): The delimiter to split on
Returns: table - Array of split strings
Example:
local words = B2Lib.Strings.split("hello,world,test", ",")
-- Returns: {"hello", "world", "test"}
trim
Removes whitespace from both ends of a string.
B2Lib.Strings.trim(str)
Parameters:
str
(string): The string to trim
Returns: string - The trimmed string
ltrim
Removes whitespace from the left side of a string.
B2Lib.Strings.ltrim(str)
Parameters:
str
(string): The string to trim
Returns: string - The left-trimmed string
rtrim
Removes whitespace from the right side of a string.
B2Lib.Strings.rtrim(str)
Parameters:
str
(string): The string to trim
Returns: string - The right-trimmed string
replace
Replaces all occurrences of a substring with a replacement string.
B2Lib.Strings.replace(str, search, replace)
Parameters:
str
(string): The string to modifysearch
(string): The substring to replacereplace
(string): The replacement string
Returns: string - The modified string
Example:
local result = B2Lib.Strings.replace("hello world", "world", "universe")
-- Returns: "hello universe"
reverse
Reverses the characters in a string.
B2Lib.Strings.reverse(str)
Parameters:
str
(string): The string to reverse
Returns: string - The reversed string
startsWith
Checks if a string starts with a specific prefix.
B2Lib.Strings.startsWith(str, prefix)
Parameters:
str
(string): The string to checkprefix
(string): The prefix to check for
Returns: boolean - True if the string starts with the prefix
endsWith
Checks if a string ends with a specific suffix.
B2Lib.Strings.endsWith(str, suffix)
Parameters:
str
(string): The string to checksuffix
(string): The suffix to check for
Returns: boolean - True if the string ends with the suffix
contains
Checks if a string contains a specific substring with optional case sensitivity.
B2Lib.Strings.contains(str, substring, ignoreCase)
Parameters:
str
(string): The string to search insubstring
(string): The substring to search forignoreCase
(boolean, optional): Whether to ignore case (default: false)
Returns: boolean - True if the string contains the substring
isEmpty
Checks if a string is empty or contains only whitespace.
B2Lib.Strings.isEmpty(str)
Parameters:
str
(string): The string to check
Returns: boolean - True if the string is empty or whitespace-only
capitalize
Capitalizes the first letter of a string and lowercases the rest.
B2Lib.Strings.capitalize(str)
Parameters:
str
(string): The string to capitalize
Returns: string - The capitalized string
toCamelCase
Converts a string to camelCase format.
B2Lib.Strings.toCamelCase(str)
Parameters:
str
(string): The string to convert
Returns: string - The camelCase string
Example:
local camel = B2Lib.Strings.toCamelCase("hello world test")
-- Returns: "helloWorldTest"
toPascalCase
Converts a string to PascalCase format.
B2Lib.Strings.toPascalCase(str)
Parameters:
str
(string): The string to convert
Returns: string - The PascalCase string
toSnakeCase
Converts a string to snake_case format.
B2Lib.Strings.toSnakeCase(str)
Parameters:
str
(string): The string to convert
Returns: string - The snake_case string
toKebabCase
Converts a string to kebab-case format.
B2Lib.Strings.toKebabCase(str)
Parameters:
str
(string): The string to convert
Returns: string - The kebab-case string
pad
Pads a string to a specified length with a character.
B2Lib.Strings.pad(str, length, padChar, direction)
Parameters:
str
(string): The string to padlength
(number): The target lengthpadChar
(string, optional): The padding character (default: " ")direction
(string, optional): Padding direction ("left", "right", "both") (default: "left")
Returns: string - The padded string
Example:
local padded = B2Lib.Strings.pad("hello", 10, "0", "left")
-- Returns: "00000hello"
truncate
Truncates a string to a specified length with optional suffix.
B2Lib.Strings.truncate(str, maxLength, suffix)
Parameters:
str
(string): The string to truncatemaxLength
(number): The maximum lengthsuffix
(string, optional): The suffix to append (default: "...")
Returns: string - The truncated string
format
Formats a string using template placeholders.
B2Lib.Strings.format(template, data)
Parameters:
template
(string): The template string with placeholdersdata
(table): The data to substitute into placeholders
Returns: string - The formatted string
Example:
local msg = B2Lib.Strings.format("Hello {name}, you have {count} messages", {
name = "John",
count = 5
})
-- Returns: "Hello John, you have 5 messages"
count
Counts occurrences of a substring in a string.
B2Lib.Strings.count(str, substring, ignoreCase)
Parameters:
str
(string): The string to search insubstring
(string): The substring to countignoreCase
(boolean, optional): Whether to ignore case (default: false)
Returns: number - The number of occurrences
random
Generates a random string of specified length and character set.
B2Lib.Strings.random(length, charset)
Parameters:
length
(number): The length of the random stringcharset
(string, optional): The character set to use (default: alphanumeric)
Returns: string - The random string
Example:
local id = B2Lib.Strings.random(8)
-- Returns: "aB3xY9mZ" (example)
escapeRegex
Escapes special regex characters in a string.
B2Lib.Strings.escapeRegex(str)
Parameters:
str
(string): The string to escape
Returns: string - The escaped string
wordWrap
Wraps text to fit within specified line length.
B2Lib.Strings.wordWrap(str, maxLength)
Parameters:
str
(string): The string to wrapmaxLength
(number): The maximum line length
Returns: table - Array of wrapped lines
similarity
Calculates similarity between two strings using Levenshtein distance.
B2Lib.Strings.similarity(str1, str2)
Parameters:
str1
(string): First string to comparestr2
(string): Second string to compare
Returns: number - Similarity score between 0.0 and 1.0
Example:
local sim = B2Lib.Strings.similarity("hello", "hallo")
-- Returns: 0.8 (80% similar)
formatCurrency
Formats a number as currency with proper separators.
B2Lib.Strings.formatCurrency(amount, currency, locale)
Parameters:
amount
(number): The amount to formatcurrency
(string, optional): The currency symbol (default: "$")locale
(string, optional): The locale for formatting (default: "en")
Returns: string - The formatted currency string
formatNumber
Formats a number with thousands separators.
B2Lib.Strings.formatNumber(number, decimals, separator)
Parameters:
number
(number): The number to formatdecimals
(number, optional): Number of decimal places (default: 0)separator
(string, optional): Thousands separator (default: ",")
Returns: string - The formatted number string
slugify
Converts a string to a URL-friendly slug.
B2Lib.Strings.slugify(str, separator)
Parameters:
str
(string): The string to slugifyseparator
(string, optional): The separator character (default: "-")
Returns: string - The slugified string
stripTags
Removes HTML/XML tags from a string.
B2Lib.Strings.stripTags(str)
Parameters:
str
(string): The string to strip tags from
Returns: string - The string without tags
toTitle
Converts a string to title case (capitalizes each word).
B2Lib.Strings.toTitle(str)
Parameters:
str
(string): The string to convert
Returns: string - The title case string
Configuration
Configure the strings module in config.lua
:
Config.Strings = {
defaultCharset = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",
defaultCurrency = "$",
defaultLocale = "en",
defaultSeparator = ",",
maxStringLength = 10000,
enableUnicodeSupport = true,
cacheFormattedStrings = true,
trimWhitespaceByDefault = true
}
Troubleshooting
String Encoding Issues
Ensure strings are properly encoded as UTF-8
Check for special characters that may cause issues
Use escape functions for regex patterns
Verify locale settings for formatting functions
Performance Problems
Cache frequently formatted strings
Use appropriate string length limits
Avoid excessive string concatenation in loops
Consider using string builders for large operations
Case Conversion Issues
Verify input strings are valid
Check for mixed character sets
Test with special characters and accents
Ensure proper locale handling
Template Formatting Errors
Check that all placeholders have corresponding data
Verify template syntax is correct
Ensure data types match expected formats
Handle missing or null values gracefully
Last updated