Hey im working with string.gsub and try to replace random letters from a string to "*" but if i use my code he replace each letter from the string.
I need for example a function with a length... so i define 3 and he just replace every third letter with "*"
here the code.
hook.Add("onChatCommand","modifychatcommand", function(ply,com,args)
for k,v in pairs(player.GetAll()) do
local col = team.GetColor(ply:Team())
if com == "advert" then
text = string.gsub( args,"%a", "*" )
return "", DarkRP.talkToPerson(v, col, DarkRP.getPhrase("advert") .." "..ply:Nick(), Color(255,255,0,255), text, ply)
end
end)
output is String = ***** and i need for example
String = S*r*n*
does someone know what i need to do... (integreating numbers would be the second step)
print(string.gsub("string.gsub pattern test 12345", "(%w)%w", "%1*")) -- s*r*n*.g*u* p*t*e*n t*s* 1*3*5
You should check the Patterns page on the wiki to learn more about patterns and how to use them.
yeah ure right i definetly have to learn this. thank you anyway.. i accept ur answer
If you want the replaced character to be random instead of every other letter or number you should use the '%s' character class and use a function in place of a string for the third argument of string.gsub.
function string.Censor(str, chance, replacement)
return (string.gsub(str, "%S", function() -- Check for any character that isn't whitespace. '%s' represents whitespace characters such as spaces, tabs, and newlines so if we use '%S' we can get everything other than whitespace.
local rand = math.random(chance) -- Pick a random number between 1 and chance.
return rand == 1 and replacement -- Replace the character with the replacement if rand is 1
end)) -- If we wrap string.gsub in parenthesis we'll only get the first return value.
end
Example outputs:
1 in 1 chance: *********** ******* **** *****
1 in 2 chance: *t*i**.g*ub p*t*e** *es* 12345
1 in 3 chance: str***.g**b p*tt*rn t*s* 1*34*
1 in 4 chance: s*ri*g.gsub p*tt*rn test 12345
1 in 5 chance: st*ing*g**b patter* test **345
1 in 6 chance: s*ri*g.gsu* *at*ern *est 12*45
1 in 7 chance: *tring.gsub p*t*ern tes* 12345
1 in 8 chance: string*gsu* patt*rn test *2*45
1 in 9 chance: s*ri*g.g*ub patt*rn test *23*5
1 in 10 chance: string.gsub p*tter* t*st 12345
oh this is usefull and easier to understand for me thank you
Sorry, you need to Log In to post a reply to this thread.