Remove duplicate spaces in a message

I’m trying to remove whitespaces from a message. Is there a function of any sort that I could use to remove them? Should be something like the .trim() function you’d get in JavaScript. If there isn’t a function like that, how would I remove the spaces?

perhaps, you could try using string.gsub
like this
string.gsub("I love Roblox", " ", "") – replaces the Spaces in the string “I love Roblox” by nothing

Wouldn’t that just get rid of all the spaces?

if you want to get rid of duplicate spaces then just do

string.gsub("I love Roblox", " ", " ")

There is meant to be a double space in the first one here, it may not show up in the editor…
So replace any double spaces with single spaces.

Might need to loop this replacement for as long as there are double spaces in the string, in case there are any triple spaces (or more).
Then if you want to also get rid of trailing and leading spaces check the first and last character, if they are spaces remove them.

1 Like

I think this code works

local str = "I     Love      Roblox"
print(({str:gsub(" +", " ")})[1])