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

How would I go about Gmatch to justify a TextLabel is empty?

Asked by
Ziffixture 6913 Moderation Voter Community Moderator
5 years ago
Edited 5 years ago

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?

0
well see, you can use string.find or string.match to find whitespaces(%s) in the string theking48989987 2147 — 5y
0
Interesting, what does gmatch do then? Ziffixture 6913 — 5y
0
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? Ziffixture 6913 — 5y
0
Edited answer User#24403 69 — 5y

1 answer

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

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

string patterns

0
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? Ziffixture 6913 — 5y
Ad

Answer this question