I want to delete several elements of a string

  1. What do you want to achieve?
    I want to delete several elements of a string
  2. What is the issue?
    I don’t know how to delete several elements of a string
  3. 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

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 :sweat_smile:

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
:grinning:

local Message = "A,New Message!"
local new_word = string.gsub(Message,"%p","")

print(new_word) -- "ANew Message"



2 Likes

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 :thinking:

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)

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

Using “%s” will detect/check if there’s space in between the letters.

1 Like

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 :slight_smile:

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)

No problems! :slight_smile:

If you want to use if statements one day with that, here’s a short example, you could take it and extend it :slight_smile:

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 :wink:
( char minimum )

1 Like