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 split

  • delimiter (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 modify

  • search (string): The substring to replace

  • replace (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 check

  • prefix (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 check

  • suffix (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 in

  • substring (string): The substring to search for

  • ignoreCase (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 pad

  • length (number): The target length

  • padChar (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 truncate

  • maxLength (number): The maximum length

  • suffix (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 placeholders

  • data (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 in

  • substring (string): The substring to count

  • ignoreCase (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 string

  • charset (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 wrap

  • maxLength (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 compare

  • str2 (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 format

  • currency (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 format

  • decimals (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 slugify

  • separator (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

  1. Ensure strings are properly encoded as UTF-8

  2. Check for special characters that may cause issues

  3. Use escape functions for regex patterns

  4. Verify locale settings for formatting functions

Performance Problems

  1. Cache frequently formatted strings

  2. Use appropriate string length limits

  3. Avoid excessive string concatenation in loops

  4. Consider using string builders for large operations

Case Conversion Issues

  1. Verify input strings are valid

  2. Check for mixed character sets

  3. Test with special characters and accents

  4. Ensure proper locale handling

Template Formatting Errors

  1. Check that all placeholders have corresponding data

  2. Verify template syntax is correct

  3. Ensure data types match expected formats

  4. Handle missing or null values gracefully

Last updated