Login | Register

Validating strings

June 28, 2006, 20:25 by Calle
[loading]
-->
Validating strings is about the definition valid, and that’s what you got to figure out at first. I made a program a while ago where I wanted to make variable names out of words, and I knew from the manual that the valid letters in a variable name was a-z, the underscore “_” and the number 0-9. Now I had to compile a list with these:

accepted = "abcdefghijklmnopqrstruvwxyz_0123456789";
As you can see I store the information in a variable. I need then to pick out the invalid letters of a string and destroy them. This approach is far more efficient than others I have seen, some of them which use the string_replace_all function. They will have to use lot more lines than I will have to.


Now this is the code to pick out and the destroy the invalid letters:

a = 0;
while(a<string_pos("=",word)) {
if (string_pos(string_char_at(word,a),accepted)==0) {
word = string_replace_all(word,string_char_at(word,a),"";);
}
a+=1;
}


Word is a variable that store the word that should be validated. I hope I needn’t to explain this anymore, but that you should understand now. I welcome you to use this script if you need to, although credits to this site is appreciated. If you like, go ahead and debate me on whether this is or is not a good solution by commenting!

Comments

Loading comments... [loading]
.
Users logged in:

game maker articles, game maker examples, game maker tutorials, gmtutorials, game maker questions and answers, game maker crash course, how to create games