From the reference libraryRedM

autodoc_shared_functions

jo_libs/modules/string/autodoc/autodoc_shared_functions.md

autodoc_shared_functions

string Methods > string:compareVersionWith()

Compare two version strings <br>

string Methods > string:compareVersionWith() > Syntax

string:compareVersionWith(version)

string Methods > string:compareVersionWith() > Parameters

version : string

The string version to compare to

string Methods > string:compareVersionWith() > Return Value

Type : integer

-1 if the version is older, 0 if it's the same and 1 if it's more recent

string Methods > string:compareVersionWith() > Example

-- Basic version comparison examples
local currentVersion = "1.2.3"

-- Compare with older version
local olderVersion = "1.1.9"
print(currentVersion:compareVersionWith(olderVersion))
-- Expected output: 1 (current version is newer)

-- Compare with same version
local sameVersion = "1.2.3"
print(currentVersion:compareVersionWith(sameVersion))
-- Expected output: 0 (same version)

-- Compare with newer version
local newerVersion = "1.3.0"
print(currentVersion:compareVersionWith(newerVersion))
-- Expected output: -1 (current version is older)


string Methods > string:convertVersion()

Convert a version string (like "1.2.3") to a numeric value <br>

string Methods > string:convertVersion() > Syntax

string:convertVersion()

string Methods > string:convertVersion() > Return Value

Type : number

The converted numeric version

string Methods > string:convertVersion() > Example

local versionString = "1.2.3"
local numericVersion = versionString:convertVersion()
print(versionString .. " converted to number: " .. numericVersion)
-- Expected output: 1.2.3 converted to number: 3002001
-- (3*1 + 2*1000 + 1*1000000)

-- This can be useful for version comparison
local newVersion = "1.3.0"
if newVersion:convertVersion() > versionString:convertVersion() then
    print("New version is greater than current version")
end


string Methods > string:firstToUpper()

Return the string with the first letter in uppercase <br>

string Methods > string:firstToUpper() > Syntax

string:firstToUpper()

string Methods > string:firstToUpper() > Return Value

Type : string

Return the string with the first letter in uppercase

string Methods > string:firstToUpper() > Example

local text = "hello world"
local capitalized = text:firstToUpper()
print(capitalized)
-- Expected output: Hello world

-- Useful for formatting names
local playerName = "john"
print("Welcome, " .. playerName:firstToUpper() .. "!")
-- Expected output: Welcome, John!


string Methods > string:removeAccent()

A function to remove all accent in a string <br>

string Methods > string:removeAccent() > Syntax

string:removeAccent()

string Methods > string:removeAccent() > Return Value

Type : string

A string without accent

string Methods > string:removeAccent() > Example

local value = "Liberté"
local newValue = value:removeAccent()
print(newValue)
-- Expected output: Liberte

string Methods > string:split()

Split a string into parts based on a delimiter <br>

string Methods > string:split() > Syntax

string:split(delimiter, pieces)

string Methods > string:split() > Parameters

delimiter : string

The character(s) to split the string on

pieces : number <BadgeOptional />

The maximum number of pieces to split into

string Methods > string:split() > Return Value

Type : table

Array of string parts

string Methods > string:split() > Example

local csvData = "apple,banana,cherry,date"
local fruits = csvData:split(",")
for i, fruit in ipairs(fruits) do
    print(i .. ": " .. fruit)
end
-- Expected output:
-- 1: apple
-- 2: banana
-- 3: cherry
-- 4: date

-- Limit the number of splits
local limitedSplit = csvData:split(",", 2)
for i, item in ipairs(limitedSplit) do
    print(i .. ": " .. item)
end
-- Expected output:
-- 1: apple
-- 2: banana,cherry,date


string Methods > string:toHex()

Convert a hexadecimal string to a number, handling signed values <br>

string Methods > string:toHex() > Syntax

string:toHex()

string Methods > string:toHex() > Return Value

Type : number

The converted numeric value

string Methods > string:toHex() > Example

local hexColor = "0xFF5733"
local colorValue = hexColor:toHex()
print("Hex " .. hexColor .. " converted to number: " .. colorValue)
-- Expected output: Hex 0xFF5733 converted to number: 16733747

-- Handle negative values (signed hex)
local signedHex = "0x80000000"
local signedValue = signedHex:toHex()
print("Signed hex " .. signedHex .. " converted to number: " .. signedValue)
-- Expected output: Signed hex 0x80000000 converted to number: -2147483648


string Methods > string:trim()

Remove whitespace from both ends of a string <br>

string Methods > string:trim() > Syntax

string:trim()

string Methods > string:trim() > Return Value

Type : string

The trimmed string

string Methods > string:trim() > Example

local paddedText = "   Hello, world!   "
local trimmed = paddedText:trim()
print("Original: '" .. paddedText .. "'")
print("Trimmed: '" .. trimmed .. "'")
-- Expected output:
-- Original: '   Hello, world!   '
-- Trimmed: 'Hello, world!'

-- Useful for cleaning user input
local userInput = "  search term   "
local cleanInput = userInput:trim()
print("Searching for: '" .. cleanInput .. "'")
-- Expected output: Searching for: 'search term'

String Functions > string.compare()

A function to compare two strings <br>

String Functions > string.compare() > Syntax

string.compare(a, b, caseSensitive)

String Functions > string.compare() > Parameters

a : string

The 1st string

b : string

The 2nd string

caseSensitive : boolean

If the compare is case sensitive<br>default: false

String Functions > string.compare() > Return Value

Type : integer

-1 if a is previous, 0 if both are same and 1 if a is after

String Functions > string.compare() > Example

local a = "automobile"
local b = "Automobile"
print(a:compare(b, true))
-- OR --
print(string.compare(a,b, true))
-- Expected output: -1 because uppercase is before lowercase

String Functions > string.spaceNumber()

Convert a integer|number to a spaced number <br>

String Functions > string.spaceNumber() > Syntax

string.spaceNumber(number, decimal)

String Functions > string.spaceNumber() > Parameters

number : integer|number

The number to convert

decimal : integer <BadgeOptional />

The number of decimal <br> default: 0

String Functions > string.spaceNumber() > Example

local number = 123456.123456
local spaced = string.spaceNumber(number, 3)
-- Expected output: "123 456.123"

string Methods > string:extractConvarComparator()

Extract resource, convar, comparator and value from a "resourceName[:convar](< > <= >= ==)value" string <br> The ":convar" part is optional (eg. "rsg-core>=2.0.0") <br>

string Methods > string:extractConvarComparator() > Syntax

string:extractConvarComparator()

string Methods > string:extractConvarComparator() > Return Value

Type : string?


Back to documentation