Hi, I have a TextLabel, more of a TextBox though, and before FocusLost
fires, I run a quick overview of what the Player wrote. Yet simply asking if TextLabel.Text == ""
won’t really do it. I understand there is a BuiltInFunction called string.gmatch()
that can rule out certain pieces of Text, I was wondering if this could be done to see if the TextLabel is not only empty, but full of spaces. Can and how would I do it?
You do not know how to check if a TextBox is empty/full of spaces. You have found a solution to your problem, that is to use the gmatch
function. You want us to help you out with your solution, rather than your actual problem.
Now, more along the lines of the spirit of your inquiry, you can instead use the match
function using the string pattern ^%s*$
. %s
represents a single whitespace, and ^
and $
are anchors, where the former matches only the beginning and the latter matches the end of a string. *
is a quantifier that matches 0 or more occurrences of the original class.
match
returns nil
if there isn't a match. In this case you want there to be no match because that means there is valid input.
if not string.match(textbox.Text, "^%s*$") then -- # ... end
To return only non whitespace characters, you can use gmatch
this time.
for char in string.gmatch(textbox.Text, "%S") do -- # every non whitespace character end