Difference between revisions of "StringIsNum"

From Second Life Wiki
Jump to navigation Jump to search
(Created page with "{{LSL Header}} 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 c…")
 
m
Line 23: Line 23:
     }
     }
      
      
     //Edit with TRUE if all characters are a number
     //Exit with TRUE if all characters are a number
     return TRUE;
     return TRUE;
}
}

Revision as of 14:59, 31 March 2012

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.

<lsl> // 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) {

   integer length = llStringLength(input);  integer i;  integer verify;
   
   for(i = 0; i < length; i++) {
       string char = llGetSubString(input, i, i);
       if(char == "0" || char == "1" || char == "2" || char == "3" || char == "4" ||
          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.
               verify = TRUE;
          
       //Exit with FALSE at the first sign of a non numerical character
       else return FALSE;
   }
   
   //Exit with TRUE if all characters are a number
   return TRUE;

}

default {

   state_entry()
   {
       llListen(0, "", llGetOwner(), "");
   }
   listen(integer channel, string name, key id, string message)
   {        
       if(StringIsNum(message)) llOwnerSay(message + " consists of numbers only.");
       if(!StringIsNum(message)) llOwnerSay(message + " has other characters in it!");
   }

} </lsl>