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?
How would I go about Gmatch to justify a TextLabel is empty?
1 answer
#You are suffering from the XY Problem.
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.
##String patterns
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
1 comment
Thanks so much! But before I accept this answer, I have one question: How would I just return the Text with the Characters only? (If someone typed a bunch of spaces and one character, have the Text show the Character only without the spaces?
This discussion is preserved as it was. New answers, comments, and votes are closed.

4 comments
well see, you can use string.find or string.match to find whitespaces(%s) in the string
Interesting, what does gmatch do then?
I also have a default TextMessage on the Box, I wan't to make sure they can't type the sentence anywhere in the message?
Edited answer