How do I detect if String is made of only spaces?

Godot Version

4.4.1.stable

Question

I’m making an online game where users can enter a name. However, I want every user to have a name. I have made it so that it detects if they didn’t put a name in, but I’m not sure how to detect if they put only spaces in to try to get around it.

1 Like

Use trim() on the string. If the resulting string is zero length, then is consisted only of whitespace.

You can use strip_edges() function.

Also you should check all String functions, if you want to do something more or different:

1 Like

Thanks. Worked great.

Is this a local game or a network game? If it’s going to be networked, users putting in only spaces is the least of your worries when it comes to vetting names…

As for the space thing, it can get complicated; unicode has a whole variety of whitespace characters, and you also potentially have the whole question of broken unicode; for example, what happens if someone abuses combining characters…

2 Likes

Z̴̹̘͈̪̪͂̌̐̈́̾̓A̴̮̼̝̔͛͌Ļ̸͇̩̝̝͎͚̠̈͂̇̀̑͂G̴͕͆̀͊́́̋̚͝O̵̢̺̠͇͔̗͕̱͋͊̃̓͐́͘̚ ̸̣͙͔̜̥͚͇̱͌̈́̌̂ĺ̷̗̗͔̣͔̄̅̎̚í̴̺̘̮̤̀̓͊͒̉̎k̷̺͎̬̭̐̆e̷͓̣̫̹̠̘͂̂̀s̴̨̥̗̱͒͐̋̂͋̌̚ͅ ̵̪̔̓̃̈́͘U̴̻̲̲̲̬̼̲̎͛͋͑̈́̒̎͜ǹ̶̤̈́̿̀i̶̦͕͎͗c̸̭͚̪͐̌̄̀͆́̆̚ǫ̷͖͓̝̪̖̯̘͂̇̓ḑ̶̍͆̀͑̓̀̆͋ȩ̵͕͑̏̆͑̏͜͝ͅ

1 Like

To @hexgrid 's point, if you want to really check this, you should be using a Regular Expression (RegEx) that is constructed to strip out unwanted characters. It can also check for spaces for you.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.