Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do I detect only spaces in strings?

I was wondering what should I do to detect if a string only contains spaces only like for example if a TextBox Text would be like:

TextBox.Text = "           "

Also, if someone does like:

TextBox.Text = "Hi          "

It would be ok since the result isn't only blank.

Not sure if string.gmatch(TextBox.Text,'+%s-%w-%p') would be right but I would need advice on how this would be executed, thanks!

2 answers

✓ Accepted answer

What I did to counter this was set up a for loop, then loop through the string and see if it isn't all strings. For example:

local AllIsSpaces = true
local String = '                  '

for i = 1, #String do
	if String:sub(i, i) ~= ' ' then
		AllIsSpaces = false
	end
end

if AllIsSpaces then
	print('It\'s all just flippin spaces! D:<<')
else
	print('Accepted! ^^')
end

There's probably better ways out there, but this's the solution I've come up with thus far. :P If you have any questions, please ask away! ^^

I hope this helped! :D

if string.match("here's a string with non-whitespace characters", "%S") then
	-- code
end

The %s character class matches all whitespace characters, and the %S character class matches the complement (all non-whitespace characters).

This discussion is preserved as it was. New answers, comments, and votes are closed.