I would like to format strings so that if I put the word st.ring it will return string. or another example s-tring will still return string. Can anyone help me with this?
do
if string.find(yourtext,".") ~= nil then
local splits = string.splits(yourtext,".")
local newstring = splits[0]..splits[2] --if this doesnt work then do splits[1]..splits[2]
end
Ok let me try it, thanks for helping me!
an easier way would be to use gsub
local str = "hell.o"
print(str:gsub("%.",""))
I noticed that if i do this, then it will put a number at the end of it. if there any way I can remove it in the same string?
Gsub returns two things, the number of replacements made, and the new string after replacement(s). You can only select the first return by capturing it (e.g. local str = str:gsub("%.". "")
) or by implicitly resizing the list to only one argument w/parenthesis (e.g. print( (str:gsub("%.", "")) )
).
Thanks, I never really played with strings that much before.
After re-reading the OP, it appears you’d like to remove all forms of punctuation, not just periods, correct?
If so, you can achieve this by using the %p
pattern; it matches all punctuation characters ( !
@
#
;
,
.
)
e.g.
local str = "str-ing"
str = str:gsub("%p", "")
print(str) --> "string"
Note that this will remove ALL punctuation (as defined above) in the string. If that’s not what you want, please let me know and I’ll try to help further.
I actually need tons of special characters to be blocked for my game. This way players can’t give something away. So I am going to put all the characters in a table.
In that case, you can make use of sets.
e.g.
local blockedCharacters = {".", "/", "`"}
local patternToMatch = "["..table.concat(blockedCharacters).."]"
local function filterStringForBlockedChars(str)
return ( str:gsub(patternToMatch, "") )
end
print(filterStringForBlockedChars("hello ./` there"))