Does string library has a use?

do roblox devs use string library like string.format? and what are the cases that the devs need the library for ?

3 Likes
str="adadw123132"
local s= sring.gsub(str,"%D","") --get all digits in a text
print(s)

I am currently watching this:

@xxmanado

Possibilities are basically endless.
From showing UIs to data handling (in some cases).

As Roblox documentation states:
“The string library provides generic functions to manipulate strings, such as to extract substrings or match patterns. You can access the string library by the global string library.”

Yes.

Returns the internal numerical codes of the characters s[i], s[i+1], …, s[j]. The default value for i is 1; the default value for j is i. These indices are corrected following the same rules of function string.sub.

Receives zero or more integers and returns a string with length equal to the number of arguments, in which each character has the internal numerical code equal to its corresponding argument.

Looks for the first match of pattern in the string s and returns the indices of s where the occurrence starts and ends.

Returns a formatted version of its variable number of arguments following the description given in its first argument, which must be a string.

Returns an iterator function that returns the next captures from pattern over the string s each time it’s called.

Returns a copy of s in which all or the first n occurrences of the pattern are replaced with the given replacement. The second value returned is the total number of substitutions made.

Returns the length of a string.

Returns a copy of a string with all uppercase letters changed to lowercase.

Looks for the first match of pattern in the string s. If a match is found, it is returned; otherwise, it returns nil. A third, optional numerical argument, init, specifies where to start the search.

Returns a binary string containing the provided arguments.

Returns the size in bytes of any string packed with a given description.

Returns a string that is the concatenation of n copies of the string s.

Returns a string that is the string s reversed.

Splits a string into parts based on the defined separator character(s), returning a table of ordered results.

Returns the substring of s that starts at i and continues until j. i and j can be negative. i defaults to 1 and j defaults to -1.

Extracts the values packed in the provided binary string.

Returns a copy of a string with all lowercase letters changed to uppercase.

The library is useful for all kinds of string manipulation.
For example:

  • string.format is useful when you have a base framework string you need to customise.
local baseString = "%s received %d Cash for completing task number %d!"

--manipulate in two scenarios
local customisedPlr1 = string.format(baseString, Player1.Name, 3, 1)
local customisedPlr2 = string.format(baseString, Player2.Name, 5, 7)
  • string.find is useful when you need to find things within a string:
local targetString = "check THIS VALUE"
if string.find(targetString, "THIS VALUE") then
    print("Found target phrase in string.")
end
  • string.match is useful when you need to retrieve a phrase from a string:
local targetString = "player just won the round!"
local found = string.match(targetString, "%a+ just")
  • string.lower() and string.upper() are useful for converting between cases
  • string.gsub is useful for replacing things within a string:
local targetString = "foo1, foo2, foo3"
targetString = string.gsub(targetString, "foo%d", "bar")

targetString = string.gsub(" ", "_%1_")
  • string.sub is useful for shortening strings, useful in things like typewriter effects:
print(string.sub("foobar", 1, 5)) --> "fooba"
  • Other functions like string.byte, string.char, etc. are useful for other things, as are these. This is just an example.
1 Like

Well i like the case that u have provided thanks for clarifying

1 Like

Guys thanks for clarifying the library but what i want to see some cases that devs actually use them

In Piggy, the dev uses string.format to create a minute:second timer system. You cannot achieve this perfectly without string.format

function MSTo(s)
   return string.format("%02i:%02i",s/60,s%60)
end
for i = 600,0,-1 do
   print(MSTo(i))
end
function MSTo(s)
    local minutes = math.floor(s / 60)
    local seconds = s % 60
    return (minutes < 10 and "0" .. minutes or minutes) .. ":" .. (seconds < 10 and "0" .. seconds or seconds)
end

for i = 600, 0, -1 do
    print(MSTo(i))
end

By perfectly I meant like the most straightforward and most optimised

1 Like

Well thanks for the usecase can u please provide more i just want to see more use cases to get hang of it

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.