Discord
Login
Community
DARK THEME

How do I compare the contents of a list to a string?

for example:

not_allowed = ["a","b","c"]

string = "bca"

if string comparable to not_allowed then
 return
end

// ( the string would be allowed ) 

In this case, you can iterate over not_allowed and check if each item is in the string.

not_allowed = ["a", "b", "c"]
string = "this string has disallowed letters"

check_string = function()
  for item in not_allowed
    if string.match(item) then
      print("not allowed")
      return
    end
  end
  print("allowed")
end

init = function()
  check_string() // not allowed
  string = "this string is fine"
  check_string() // allowed
end

If not_allowed doesn't include any multi-character strings, you can also iterate over the string.

check_string = function()
  for char in string
    if not_allowed.includes(char) then
      print("not allowed")
      return
    end
  end
  print("allowed")
end

Post a reply

Progress

Status

Preview
Cancel
Post
Validate your e-mail address to participate in the community