I’m working with an API and I only want allowed characters through a header. For example, I want to block out characters such as %, and ?.
So this sentence “Hello, how are you?”
Would turn into “Hello, how are you”
Take all the unwanted characters and store them in an array. Then what you wanna do is use string.gsub()
, here is an example:
local str = "Hello, how are you?"
local str = string.gsub(str, "?", "") --> First argument is the string, second argument is what we are looking for, third agrument is replacing it with empty string.
print(str) --> This is gonna print 'Hello, how are you'
1 Like
Do you know of a list of all of the characters somewhere?
Use string.gsub
to remove words that you don’t want.
local str = '%%Hey this is a test?'
local new = string.gsub(str, '[%%%?]+', '')
print(new) --> Hey this is a test
1 Like
You can actually simplify this by using the string pattern %p
, which will match any punctuation character.
Modified code:
local str = '%%Hey this is a test?'
local new = string.gsub(str, '%p', '')
print(new) --> Hey this is a test
2 Likes
I found something that is working
str:match("[%P]+")