List variable that contains a bunch of words? (sorry for sounding stupid lol)

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By RoguePanda

I’m currently creating a game that contains a chat feature. I’m working on the swear filter right now. I’m using replace() and right now it looks like: text.replace(“naughty word”, “* * *”) but to get all the words that would be a bunch of lines of code. So I was wondering if I could store all the bad words into a variable, and then it would look something like:

var swear_words = (however you create a list)

text.replace(swear_words, “* * *”)

This always makes me laugh. You don’t want swear words in your game so you … Drumroll please … Put all known swear words under the sun in your game. Lol.

So a tip, replace the word(s) with an empty string because
A ***ing sentence like this is pretty suggestive
And another @$#& one like this ain't any better

Wakatta | 2021-05-19 03:28

:bust_in_silhouette: Reply From: exuin

You’re looking for an Array (or PoolStringArray). You will need to loop through the array and check every word to replace, however.

:bust_in_silhouette: Reply From: magicalogic

You need to use an array and a for loop as follows :

text = "Your string with swear words" 
var swear_words = ["word_1","word_2","word_3"]
for word in swear_words:
    text.replace(word, "****")

Replace the contents of text and swear_words with your text and swear words and it should work.

:bust_in_silhouette: Reply From: aipie

The provided answers will work were you loop through the array and replace the text for each swear word.

You could also use a regex to replace the swear words in one replacement.

var r = RegEx.new()
r.compile("swear1|swear2|swear3")
var string = "This is a text full of swear words like swear1 or swear2 and even swear3. It even gets repeated swear1, swear2"
var replacedstring = r.sub(string, "* * *", true)
print(replacedstring)

This prints

This is a text full of swear words like * * * or * * * and even * * *. It even gets repeated * * *, * * *