Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
2

How would I replace text in a string no matter what the case is without changing the strings case?

Asked by 9 years ago

I am trying to make a custom chat and I want a word blacklist (words that can't be said so they get replaced with #'s), I have tried and it worked but when it did it it also changed the whole message's case and I couldn't see any way around this. This is an example of what I did:

v = "LOL"
msg = "Hi, I like to LOL because I can"
print(string.gsub(string.lower(msg),string.lower(v),string.rep("#",string.len(v))))
-- The output is: hi, i like to ###  because i can

My problem is that it changes the whole message to lower case and if I try to fix it by not changing the message to lower case this is the script I could use:

v = "lol"
msg = "Hi, I like to LOL because I can"
print(string.gsub(msg,string.lower(v),string.rep("#",string.len(v))))
-- The output is: Hi, I like to LOL because I can

My problem here is that if the message has the word but in a different case and the only way I can think of fixing this is having a table of all the possible cases but if I do a long word like intermediate it has 100's of possibilities and that's just one word. If you know a quick way to do this please help me!

0
I think I might know the answer to my own question, I'll just test my theory but please if you know post the answer! General_Scripter 425 — 9y
1
string.find(msg:lower(),v:lower()) will help you here. Find the part of the string found to be v and change it to # or do you code, and only lower case that part of the string. For more Info: http://wiki.roblox.com/index.php?title=String.match#string.find FutureWebsiteOwner 270 — 9y
0
That's exactly what my theory was, I was going to do it but I thought it might not work so continued with my main script. Now you have said the same thing I will test it, if you post that as an answer I will vote it up and accept answer to give you some reputation. General_Scripter 425 — 9y
0
I'll try to find a solution for you and then test it in Studio. However, my answer will not be posted for at least 5 from now. Sorry; I'm in school. However, if I were you, I would try to do something that finds all matches of "LOL" in any case in a string, then use string.sub to replace them with #'s. I'm not sure if that can be done, because as said earlier, I can't test in Studio currently. yumtaste 476 — 9y

1 answer

Log in to vote
-1
Answered by 9 years ago

This script should work to sum up what it does, it converts the string to lower text then removes all of the blocked text then compares the processed text with the users message and converts the changes.

I have also added hashes for the text length so it will increase and decrease with the text length.

I will add a comments and explain it if you need me too, as I don't have much spare time atm.

local blkList = {'lol','ok'} --All needs to be in lower case, dont worry it will be converted to upper case later :P
local message = "Hallo lol ok Lol"



function check(msg)

    local lowMsg = string.lower(msg)
    local procMsg = lowMsg
    for i, v in pairs(blkList) do

        for w in string.gmatch(lowMsg, string.lower(v)) do

            local len =  string.len(w)          
            local txt = string.rep("#", len)
            procMsg = string.gsub(procMsg, v, txt)
        end

    end

    local outStr = ""
    for i = 1, string.len(msg) do

        if string.sub(procMsg, i,i) == "#" then
            outStr = outStr .. string.sub(procMsg, i,i)
        else
            outStr = outStr ..  string.sub(msg, i, i)
        end

    end


    return outStr

end

print(check(message))
Ad

Answer this question