|
Delete Blank
/* Cuts the Whitespace from Stringend */ String.prototype.delBlank = function() { for (var i = 0; i<=this.length; i++) { if (this.slice(this.length-1, this.length) == " ") { this = this.slice(0, this.length-1); } } return (this); }; testString = "This is my text "; testString = (testString.delBlank()); trace(testString+"|"); // traces: This is my text| Posted by: andihaas | website http://www.flash.andihaas.de |
// Complete email validation function xValidateEmail(xES):Boolean{ var xES:String = xES.toLowerCase(); var xVE:Boolean = true; var xA:Number = xES.indexOf("@"); var xB:String = xES.substring(0,xA); var xD:Number =xES.length-1; var xE:Number =xES.lastIndexOf("."); var xC:String = xES.substring(xA+1,xE); var xF:Number =(xD)-(xE+1); if(xA==-1){xVE=false;} if(xA != xES.lastIndexOf("@")){xVE=false;} if(xCheckSUVC(xB)!=true){xVE=false;} if(xB.length<1){xVE=false;} if(xCheckSUVC(xC)!=true){xVE=false;} if(xC.length<2){xVE=false;} if(xCheckSUVC(xES.substr(xE+1,xD))!=true){xVE=false;} if(xF<1 || xF>3){xVE=false;} return xVE; } function xCheckSUVC(xESS):Boolean{ var xV:Boolean = true; var xUC:Array = new Array("!","£","$","%","^","&","*","_","+","=","?",":",";","'"," "," ",'"',"~","#","/"); for(var i:Number=0;i<=xUC.length-1;i++){ if(xESS.indexOf(xUC[i],0)!=-1){ xV=false; break; } } return xV; } var xEmailIsValid:Boolean = xValidateEmail("info@hk510.com"); trace("The email is valid = "+xEmailIsValid); stop(); Posted by: HK510 | website http://www.hk510.com |
String.prototype.retVal = function() { //-------This bit of code will break any string of arithmetic down to a solution //-------returning a real number solving in the order of "multiply divide add subract". //-------This was to compensate for the fact that the Eval statement in flash doesnt //-------do this (as it does in many other languages). The function will not handle //-------parenthesis, perhaps in a later version if I get any requests. //-------Created by Ryan Cameron www.northeastmagic.com ryan@northeastmagic.com //-------Have fun, modify at will! I did this quickly to solve an immediate issue, //-------so Im aware its clunky, but it seems to do the trick, if there is a bug please let me know. temp_math_array = new Array(); var tempHolder = ""; var temp_math_arrayCount = 0; for (var u = 0; u<=this.length; u++) { cc = this.charAt(u); if (cc eq "+" || cc eq "-" || cc eq "*" || cc eq "/") { temp_math_array[temp_math_arrayCount] = tempHolder; temp_math_arrayCount++; temp_math_array[temp_math_arrayCount] = cc; temp_math_arrayCount++; tempHolder = ""; } else { tempHolder = tempHolder+cc; } temp_math_array[temp_math_arrayCount] = tempHolder; } var numArrayCount = 0; var maxCount = 0; while (Number(temp_math_array.length)>1) { maxCount++; if (numArrayCount>=Number(temp_math_array.length)) { numArrayCount = 0; } prev = Number(numArrayCount-1); post = Number(numArrayCount+1); divideEnabled = true; plusEnabled = true; minusEnabled = true; for (var inCount = 0; inCount<temp_math_array.length; inCount++) { if (temp_math_array[inCount] eq "*") { divideEnabled = false; plusEnabled = false; minusEnabled = false; } if (temp_math_array[inCount] eq "/") { plusEnabled = false; minusEnabled = false; } if (temp_math_array[inCount] eq "+") { minusEnabled = false; } } switch (temp_math_array[numArrayCount]) { case "+" : if (plusEnabled) { temp_math_array[prev] = Number(Number(temp_math_array[prev])+Number(temp_math_array[post])); temp_math_array.splice(numArrayCount, 2); } break; case "-" : if (minusEnabled) { temp_math_array[prev] = Number(temp_math_array[prev]-temp_math_array[post]); temp_math_array.splice(numArrayCount, 2); } break; case "*" : temp_math_array[prev] = temp_math_array[prev]*temp_math_array[post]; temp_math_array.splice(numArrayCount, 2); break; case "/" : if (divideEnabled) { temp_math_array[prev] = Number(temp_math_array[prev])/Number(temp_math_array[post]); temp_math_array.splice(numArrayCount, 2); } break; default : break; } numArrayCount++; } return temp_math_array.join(""); }; myString = "1+8*5/6+3*6+2"; trace(myString+" = "+myString.retVal()); //outputs 1+8*5/6+3*6+2 = 27.6666666666667 Posted by: ryan cameron | website http://www.northeastmagic.com |
// extracts all numerals form string and returns them as a Number object function extractNumber (str:String){ var char:String; var num:String = ""; for(var i:Number = 0; i<str.length; i++){ char = str.substr(i, 1); if(!isNaN(char)){ num+=char } } return Number(num); } var inputStr:String = "something like 1agssag24dlfq230kf7"; var num:Number = extractNumber(inputStr); trace(num); Posted by: artoix | website http://www.artoix.com |
/*I made this small module for the open source project we're going to do.
It's much like the name completion module from mIRC
(which is of course modelled on the bash UNIX environment's TAB command).
When executed over a partial string it searches for a match in a list of names.
If it find a (non-case-sensitive) match, it returns it.
If it finds the exact same string it returns the next match (if any).
Those of you who use mIRC would be aware that if you type "Joe" and there's a "joe"
online it will change your prompt to "joe" (note the case difference), but if you type "Joe"
and there's a "Joe" online, if there is also a "Joe2" or a "JoeRT" online, it
will skip to their name... For those of you who don't know what I'm on about at
all, in mIRC you can type the first frew
letters of an online user's name and press Tab and mIRC will complete the name for you..
it just makes calling someone by name that much easier. Enough talk, here's the code.
I've not optimized it but with B. Hall's String Object it should be way fast. */
names = ['Andy', 'Andy2', 'Bobby', 'Catey', 'Doogey'];
function matchName (partial) {
for (var k = 0; k<names.length; k++) {
if (names[k] == partial) {
continue;
} else if (names[k].toLowerCase() == partial.toLowerCase()) {
return names[k];
}
if ((names[k].substr(0, partial.length)).toLowerCase() == partial.toLowerCase()) {
trace ("found possible match!");
return names[k];
}
}
return null;
}
trace (matchName('And'));
trace (matchName('and'));
trace (matchName('andy'));
trace (matchName('Andy'));
trace (matchName('Anc'));
Posted by: Jesse Stratford | website http://www.actionscript.org |

