Information
By: Glen
Asked: April 07, 2011
Answered: December 31, 1969
Worth: 10 Member Points
Question answered:
The member points were handed out this way:

Cpsgames (20)
JID (0)
PY (0)
sk8m8trix (0)
KaBob799 (0)
How do you check a string for letters? 10 points.

To GM both of these strings are not empty.

str = "text";
str = " ";

How do I check to make sure the string has letters and isn't just empty spaces? I'm writing scripts for a chatbox and i quickly spammed the keyboard to see how fast i could send messages and some of the messages were sending when I pressed spacebar + enter. It would send an empty message that was a few spaces. I want to check for characters. The message should have at least a letter in it. Any ideas?


Dev - Warning
Dev - After editing rating, make rating display change
Dev - When editing, update "edited" column
Dev - Restore deleted comments
Dev - Display deleted comments if mod (hidden, then with dropdown)
Dev - After deletion or during edit, make rating dropdown appear again
Dev - Stricter rating rules. Prevent user from rating again
Dev - Pages
Dev - Reporting
Do
Code
if string_length(str) = 0{*insert action here*}


O wait nvm, I see what you're asking for, disregard my comment, it has never happened.
Posted by JID April 07, 2011 0:07 - 2.1 years ago
| [#01]

Yea, if you type a few spaces in the chatbox and press send, keyboard_string technically isn't empty anymore and it sends those spaces.
Posted by Glen April 07, 2011 0:16 - 2.1 years ago
| [#02]

Easy, just trim all of the blanks out (" ").

Code
str = string_replace_all(str," ","");


Then check the length. This will prevent people from sending blank messages like you said.

Edit: To make this easier to understand.

Code
chatstr = keyboard_string;

len = string_replace_all(chatstr," ","");

if(string_length(len) < 1)
{
 //Derp, blank
}
else
{
//send chatstr
}
Posted by Cpsgames April 07, 2011 0:30 - 2.1 years ago
| [#03]

I made a script that works, but I feel it's way to long for this.

Code
notempty = false;
for (i=0; i<string_length(keyboard_string);i+=1)
    {
        switch(string_char_at(keyboard_string,i))
            {
            case 'a': { notempty = true; } break;
            case 'b': { notempty = true; } break;
            case 'c': { notempty = true; } break;
            case 'd': { notempty = true; } break;
            case 'e': { notempty = true; } break;
            case 'f': { notempty = true; } break;
            case 'g': { notempty = true; } break;
            case 'h': { notempty = true; } break;
            case 'i': { notempty = true; } break;
            case 'j': { notempty = true; } break;
            case 'k': { notempty = true; } break;
            case 'l': { notempty = true; } break;
            case 'm': { notempty = true; } break;
            case 'n': { notempty = true; } break;
            case 'o': { notempty = true; } break;
            case 'p': { notempty = true; } break;
            case 'q': { notempty = true; } break;
            case 'r': { notempty = true; } break;
            case 's': { notempty = true; } break;
            case 't': { notempty = true; } break;
            case 'u': { notempty = true; } break;
            case 'v': { notempty = true; } break;
            case 'w': { notempty = true; } break;
            case 'x': { notempty = true; } break;
            case 'y': { notempty = true; } break;
            case 'z': { notempty = true; } break;
            case '0': { notempty = true; } break;
            case '1': { notempty = true; } break;
            case '2': { notempty = true; } break;
            case '3': { notempty = true; } break;
            case '4': { notempty = true; } break;
            case '5': { notempty = true; } break;
            case '6': { notempty = true; } break;
            case '7': { notempty = true; } break;
            case '8': { notempty = true; } break;
            case '9': { notempty = true; } break;
            case '!': { notempty = true; } break;
            case '@': { notempty = true; } break;
            case '#': { notempty = true; } break;
            case '$': { notempty = true; } break;
            case '%': { notempty = true; } break;
            case '^': { notempty = true; } break;
            case '&': { notempty = true; } break;
            case '*': { notempty = true; } break;
            case '(': { notempty = true; } break;
            case ')': { notempty = true; } break;
            case '-': { notempty = true; } break;
            case '+': { notempty = true; } break;
            case '=': { notempty = true; } break;
            case '?': { notempty = true; } break;
            case '<': { notempty = true; } break;
            case '>': { notempty = true; } break;
            }
}


I'm going to try your str replace method real quick.
Posted by Glen April 07, 2011 0:31 - 2.1 years ago
| [#04]

Okay, your way worked CPS.

Code
var str;
str = string_replace_all(keyboard_string," ","");
if (string_length(str)>0) 
    {
        notempty = true;
    }
else notempty = false;


did the same thing as my really long brute force script.
Posted by Glen April 07, 2011 0:34 - 2.1 years ago
| [#05]

Neat. :D
Posted by Cpsgames April 07, 2011 0:36 - 2.1 years ago
| [#06]

There's a difference between getting the job done, and doing it efficiently. :P Glad there was a shorter way.
Posted by Glen April 07, 2011 0:38 - 2.1 years ago
| [#07]

You could also iterate through every letter and check to see if there are any non-blacklisted characters in it, and then return true if you come across one. Has the advantage of less string replacing, which I've always found messy for validation.
Posted by PY April 07, 2011 3:46 - 2.1 years ago
| [#08]

you could do string_lettersdigits(str) and check the length of that and if it's not 0 send the string.
Posted by sk8m8trix April 07, 2011 5:06 - 2.1 years ago
| [#09]

@sk8
that technique becomes very annoying when your trying to use smilies. But it could be used to simplify the check leaving just special characters to be checked if it is 0 long.
Posted by KaBob799 April 07, 2011 5:13 - 2.1 years ago
| [#10]

Recent Activity
 
Active Users (0)