Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

How do I detect only spaces in strings?

Asked by
yoshi8080 445 Moderation Voter
6 years ago

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

Log in to vote
1
Answered by 6 years ago
Edited 6 years ago

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

Ad
Log in to vote
1
Answered by
Avigant 2374 Moderation Voter Community Moderator
6 years ago
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).

Answer this question