Difference between revisions of "StringIsNum"

From Second Life Wiki
Jump to navigation Jump to search
m (Tidied code and added more comments)
m (<lsl> tag to <source>)
 
(5 intermediate revisions by 2 users not shown)
Line 5: Line 5:
This snippet is a fully working User Made Function. It is designed to be inserted into existing scripts to check if an input consists entirely of numbers, and will reject inputs that contain letters or symbols.
This snippet is a fully working User Made Function. It is designed to be inserted into existing scripts to check if an input consists entirely of numbers, and will reject inputs that contain letters or symbols.


<lsl>
Note: This is a very inefficient technique. See later better examples.
// This function returns TRUE or FALSE depending on if the input string consists entirely of numbers.
// It will return TRUE if the entire string is an integer number (whole number).
// It will return FALSE if the entire string is not an integer. Floating points, letters, symbols, and spaces will make it return FALSE.


integer StringIsNum(string input) {
<source lang="lsl2">
    integer length = llStringLength(input);  integer i;  integer verify;
// this function will return TRUE if the entire string consists of number characters only
   
integer string_is_num(string input)
     for(i = 0; i < length; i++) {
{
        string char = llGetSubString(input, i, i);
     list numberCharacters = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"];
    integer stringLength = llStringLength(input);


         //Compare each character in the string. For long strings, this may take time, and consume memory.
    integer index;
         if(char == "0" || char == "1" || char == "2" || char == "3" || char == "4" ||
    do
          char == "5" || char == "6" || char == "7" || char == "8" || char == "9")
    {
                //Do something. You can do whatever you like here, the variable is just a placeholder to keep the compiler happy.
         string character = llGetSubString(input, index, index);
                verify = TRUE;
 
         
         if (llListFindList(numberCharacters, [character]) == -1)
         //Exit with FALSE at the first sign of a non numerical character
            return FALSE;
        else return FALSE;
 
         ++index;
     }
     }
      
     while (index < stringLength);
    //Exit with TRUE if all characters are a number
 
     return TRUE;
     return TRUE;
}
}
//Below code is an example on how to use it. Since the returned value is a Boolean (0 or FALSE, 1 or TRUE) it can be used easily in if statements.


default
default
Line 36: Line 33:
     state_entry()
     state_entry()
     {
     {
         llListen(0, "", llGetOwner(), "");
        key owner = llGetOwner();
         llListen(0, "", owner, "");
     }
     }


     listen(integer channel, string name, key id, string message)
     listen(integer channel, string name, key id, string message)
     {      
     {
         //Respond if the string is a number
         //Respond if the string is a number
         if(StringIsNum(message)) llOwnerSay(message + " consists of numbers only.");
         if( StringIsNum(message) )
            llOwnerSay("'" + message + "' consists of numbers only.");
        else
            llOwnerSay("'" + message + "' does not consist of numbers only.");
    }
}
</source>
 
==Better Methods==
 
Here's a simpler solution for strings containing integer values from −2147483648 and 2147483647 written without + sign, leading zeros, or thousands separators ','  (Omei Qunhua)
 
<source lang="lsl2">
 
    if ( (string) ( (integer) data) == data)
        llOwnerSay("'" + data + "' contains a valid integer");
 
</source>
 
The following examples will validate that a string contains only the characters 0 though 9. (Omei Qunhua)


        //Respond if the string is NOT a number
a) Example for a string of length 5
         if(!StringIsNum(message)) llOwnerSay(message + " has other characters in it!");
<source lang="lsl2">
    StringOf5Dec(string test)
    {
         return ( (integer) ("1" + test) >= 100000);
     }
     }
}
</source>
</lsl>
 
b) Example for a string of length 1 through 9
<source lang="lsl2">
    VarStringIsDecimal(string test)
    {
        integer limit = (integer) llPow(10.0, llStringLength(test) );
        return ( (integer) ("1" + test) >= limit);
    }
</source>

Latest revision as of 19:06, 24 January 2015

Due to my need of wanting a nice clean function to test an input and check if it consists entirely of numbers, I decided to write one myself, and share with the community.

This snippet is a fully working User Made Function. It is designed to be inserted into existing scripts to check if an input consists entirely of numbers, and will reject inputs that contain letters or symbols.

Note: This is a very inefficient technique. See later better examples.

// this function will return TRUE if the entire string consists of number characters only
integer string_is_num(string input)
{
    list numberCharacters = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"];
    integer stringLength = llStringLength(input);

    integer index;
    do
    {
        string character = llGetSubString(input, index, index);

        if (llListFindList(numberCharacters, [character]) == -1)
            return FALSE;

        ++index;
    }
    while (index < stringLength);

    return TRUE;
}

default
{
    state_entry()
    {
        key owner = llGetOwner();
        llListen(0, "", owner, "");
    }

    listen(integer channel, string name, key id, string message)
    {
        //Respond if the string is a number
        if( StringIsNum(message) )
            llOwnerSay("'" + message + "' consists of numbers only.");
        else
            llOwnerSay("'" + message + "' does not consist of numbers only.");
    }
}

Better Methods

Here's a simpler solution for strings containing integer values from −2147483648 and 2147483647 written without + sign, leading zeros, or thousands separators ',' (Omei Qunhua)

    if ( (string) ( (integer) data) == data)
        llOwnerSay("'" + data + "' contains a valid integer");

The following examples will validate that a string contains only the characters 0 though 9. (Omei Qunhua)

a) Example for a string of length 5

    StringOf5Dec(string test)
    {
        return ( (integer) ("1" + test) >= 100000);
    }

b) Example for a string of length 1 through 9

    VarStringIsDecimal(string test)
    {
        integer limit = (integer) llPow(10.0, llStringLength(test) );
        return ( (integer) ("1" + test) >= limit);
    }