What do you want to achieve?
I want to delete several elements of a string
What is the issue?
I don’t know how to delete several elements of a string
What solutions have you tried so far?
game.ReplicatedStorage.Message.OnClientEvent:connect(function(player,message)
MessageSize = string.len((string.gsub(message, " ", "")))/2
Character = player.Character
end)
1 Like
Valkyrop
(aHuman)
April 7, 2022, 8:01pm
2
To remove 1 element from a string:
local word = "hello"
local new_word = string.gsub(word,"h","")
print(new_word) --> "ello"
2 Likes
Sorry it’s an error i know already how to delete one lement of a string
Valkyrop
(aHuman)
April 7, 2022, 8:03pm
4
You could remove several elements in several ways, a simple one is :
local word = "ehelor"
local new_word = string.gsub(word,"hel","")
print(new_word) --> "eor"
Another one :
local Message = "A New Message"
local new_word = string.gsub(Message,string.sub(Message,0,6),"")
print(new_word) -- "Message"
1 Like
I want to delete all of the punctuation
Valkyrop
(aHuman)
April 7, 2022, 8:07pm
6
local Message = "A,New Message!"
local new_word = string.gsub(Message,"%p","")
print(new_word) -- "ANew Message"
2 Likes
Valkyrop
(aHuman)
April 7, 2022, 8:08pm
7
Using “%p” will detect any punctuation, and with that, you can decide to ignore/remove it.
1 Like
Thanks but I currently have code that removes spaces, how do I combine them
Valkyrop
(aHuman)
April 7, 2022, 8:09pm
9
Could you show here the code? it can help
2 Likes
game.ReplicatedStorage.Message.OnClientEvent:connect(function(player,message)
MessageSize = string.len((string.gsub(message, " ", "")))/2
Character = player.Character
end)
Valkyrop
(aHuman)
April 7, 2022, 8:12pm
11
local Message = "Hi OMG!!"
local new_word = string.gsub(Message,"%p","")
local new_word2 = string.gsub(new_word,"%s","")
print(new_word2) -- "HiOMG"
1 Like
Valkyrop
(aHuman)
April 7, 2022, 8:12pm
12
Using “%s” will detect/check if there’s space in between the letters.
1 Like
Valkyrop
(aHuman)
April 7, 2022, 8:13pm
13
you could also use if statements, to check if there is actually space/punctuation in the string.
If it helped, please mark it as a solution
2 Likes
Thank you so much it does work, here my code :
game.ReplicatedStorage.Message.OnClientEvent:connect(function(player,message)
Message = (string.gsub(message, "%s", ""))
MessageSize = string.len((string.gsub(Message, "%p", "")))/2
Character = player.Character
end)
Valkyrop
(aHuman)
April 7, 2022, 8:17pm
15
No problems!
If you want to use if statements one day with that, here’s a short example, you could take it and extend it
local Message = "H I"
local new_word = string.gsub(Message,"%p","")
local new_word2 = string.gsub(new_word,"%s","")
if Message:match("%p") then
print("removed punctuation")
elseif Message:match("%s") then
print("narrowed space")
end
1 Like
Ok thank you
( char minimum )
1 Like