How to effectively remove whitespaces from text

Hello everyone!
It’s late at night and I got the sudden urge to write a tutorial, so here we are!
Today (or tonight I guess :smiley:) we are going to look into whitespaces in strings and how we can remove them!

A whitespace is basically a character thats “invisible” - And therefore it’s either used to separate words from each other, or to bypass the rules when signing up somewhere in an attempt to get a cool “blank name”.

Anyway, let’s cut to the chase.
Have you ever had the problem where you need to make a search bar or an username selector and want to eliminate the possibility of people typing in a million spaces?

Fear not! With the power of string.gsub(), we can solve this problem easily!

local cleanString = string.gsub(textbox.Text, " ", "") -- Remove all spaces

Then proceed to add an if statement that checks the length of the string:

if (#cleanString > 0) then
     print("You actually typed something! YEEEEAAAAAHHHHH!!!!!")
end

That’s it! That’s all I wanted to put out there.
Take care! :smiley:

1 Like
local text = "Hello	there"
local cleanString = string.gsub(text, " ", "")
print(cleanString)

image

You should use text:gsub("%s" , "") instead as this removes every type of whitespace instead of just regular spaces.

4 Likes