Home » Actionscripts library » String Object
|
10 times faster String.split()-function
//about 10 times faster String.split()-function
String.prototype.split = function(str)
{
if(str.length>1)return this.OldSplit(str);
if (str == "" || str == null) return this.toCharArray();
var result = new Array();
var d = this.length;
var n = 0;
var tmpStr = "";
var ch;
while(n<d)
{
ch = substring(this ,++n, 1);
if(ch==str)
{
result.push(tmpStr);
tmpStr="";
} else{
tmpStr += ch;
}
}
result.push(tmpStr);
return result;
}
st.toCharArray = function()
{
var st = this;
var arr_result = new Array();
for (var s=0;s < st.length;s++)
{
arr_result.push(substring(this, s+1, 1));
}
return arr_result;
}
// and with big numeric arrays I have used this:
String.prototype.split = function(str)
{
var result = new Array();
var d = this.length;
var n = 0;
var tmpStr = "";
var ch;
while(n<d)
{
ch = substring(this ,++n, 1);
if(ch==str)
{
result.push(Number(tmpStr));
tmpStr="";
} else{
tmpStr += ch;
}
}
result.push(Number(tmpStr));
return result;
}
Posted by: Lauri Koutaniemi | website http://www.minikarting.com/en/ |
Copy and Paste your script hereon(press)
{
var alphaCount=0
var numCount=0
var num_valid="123456789"
var text2 = my_text1.text.toLowerCase()
for (var i=0; i<text2.length; i++) {
if (num_valid.indexOf(text2.charAt(i)) < 0) {
numCount++
}
}
if(numCount==text2.length){
trace("Your password contains only characters. Please enter an alphanumeric password");
//upassID.focus();
return false;
}
var alph_valid="abcdefghijklmnopqrstuvwxyz"
for (var i=0; i<text2.length; i++) {
if (alph_valid.indexOf(text2.charAt(i)) < 0) {
alphaCount++
}
}
if(alphaCount==text2.length){
trace("Your password contains only numbers. Please enter an alphanumeric password" );
//upassID.focus();
return false;
}
trace("Neeraj");
return true;
}
Posted by: Neeraj Shrivastava | website http://www.tatainfotech.com |
/************ /* String arithmetic parsing function - abeall.com /* takes a string like "1.5+(500-2)/7+((25+3)*.2)" and does the arithmetic /* - it works with proper respect to parentheses /* - it will also parse variables, ex. "2*_root.myvar" /* - ignores white space, including line breaks /* returns: Number /************/ _global.arithmeticParser = function(str){ if(typeof str!='string')return str; //ignore white space str = str.split(' ').join('').split('\n').join('').split('\r').join(''); //parse any variables to values str = parseVariables(str); //CORE ALGORITHM/////////////////////////////////// //create nested arithmetic array based on parenthesis //ex: 1.5+(500-2)/7+((25+3)*.2) => ['1.5','+',['500','-','2'],'/','7','+',[['25','+','3'],'*','.2']] var arithArr = []; var currStr = ""; var arrayScope = [arithArr]; for(var i=0 ; i<str.length ; i++){ var char = str.charAt(i); var currArr = arrayScope[arrayScope.length-1]; if(char=="("){ if(currStr!="")combineArray(currArr,currArr.length,arithStrToArray(currStr.split("(").join(''))); currStr = ""; arrayScope.push(currArr[currArr.length]=new Array()); }else if(char==")"){ combineArray(currArr,currArr.length,arithStrToArray(currStr.split(")").join(''))); currStr = ""; arrayScope.pop(); }else{ currStr = currStr + char; } } combineArray(currArr,currArr.length,arithStrToArray(currStr)); //dump(arithArr); str = arrayArithmetic(arithArr); return str; //FUNCTIONS//////////////////////////////////////// //performs arithmetic() to 3D arrays from deepest array to the top array function arrayArithmetic(ar){ for(var i in ar){ if(typeof ar[i]=='object'){ ar[i] = arrayArithmetic(ar[i]); } } return arithmetic(ar); } //splices all elements in the Array 'insertArr' into the Array 'ar' at 'index' function combineArray(ar,index,insertArr){ for(var i in insertArr){ ar.splice(index,0,insertArr[i]); } } //converts arithmetic string to an array //ex: 7+2/8-12 => ['7','+','2','/','8','-','12'] function arithStrToArray(arithStr){ var ar = []; var currStr = ""; for(var i=0 ; i<arithStr.length ; i++){ var char = arithStr.charAt(i); if(char=="/" || char=="*" || char=="-" || char=="+"){ if(currStr!="")ar.push(currStr); ar.push(char); currStr = ""; }else{ currStr = currStr+String(char); } } if(currStr!="")ar.push(currStr); return ar; } //performs the four basic arithmetic operations to an array function arithmetic(ar){ for(var i=0 ; i<ar.length ; i++){ if(ar[i]=="/"){ var n = Number(ar[i-1]) / Number(ar[i+1]); //perform operation ar.splice(i-1,3,n); //splice out the [number,operation,number] subarray and replace with the answer number i-=1; //since the array length has been modified(spliced) the operation must back up to get all further operations } } for(var i=0 ; i<ar.length ; i++){ if(ar[i]=="*"){ var n = Number(ar[i-1]) * Number(ar[i+1]); ar.splice(i-1,3,n); i-=1; } } for(var i=0 ; i<ar.length ; i++){ if(ar[i]=="-"){ var n = Number(ar[i-1]) - Number(ar[i+1]); ar.splice(i-1,3,n); i-=1; } } for(var i=0 ; i<ar.length ; i++){ if(ar[i]=="+"){ var n = Number(ar[i-1]) + Number(ar[i+1]); ar.splice(i-1,3,n); i-=1; } } return ar[0]; //the array should be down to one element: the answer } //replace variable references in str with actual variable values function parseVariables(str){ var currStr = ""; var parsingObject = false; var recursions = 0; for(var i=0 ; i<str.length ; i++){ var c = str.charAt(i); //break on numbers *only* if not already parsing object to respect vars with numbers in the name //and on '.' only when not parsing an object to respect decimals vs. object paths var n = parsingObject ? true : (isNaN(c) && c!=".") ; if(n && c!="/" && c!="*" && c!="-" && c!="+" && c!="(" && c!=")" && c!=" "){ //if not on breaking char if(!parsingObject)parsingObject = true; currStr = currStr+String(c); //add to string }else if(parsingObject){ //if hit breaking char and currently parsing object str = str.split(currStr).join(String(eval(currStr))); //replace object string with value i -= currStr.length; //since the string length has changed, must back up parsingObject = false; currStr = ""; } } if(currStr!="")str = str.split(currStr).join(String(eval(currStr))); //add last var if needed return str; } } //show multi dimensional objects/arrays with indented children function dump(obj,indent){ if(!indent)indent=""; for(var i=0 ; i<obj.length ; i++){ if(typeof obj[i] == 'object'){ dump(obj[i],indent+" "); }else{ trace(indent+i+":"+obj[i]); } } } //example blah = 53; woo = 12; wha = 12; this.createEmptyMovieClip("pluh",1).arg = 100; myString = "(7.123 + 2 / (_root.blah + _level0.woo) * wha)* pluh.arg"; trace(myString+" = "+arithmeticParser(myString)); trace((7.123 + 2 / (_root.blah + _level0.woo) * wha)* pluh.arg ); //check math Posted by: Aaron Beall | website http://abeall.com |
// Function filters the wanted words ourt of the text // http://flash.andihaas.de String.prototype.changer = function(fil) { var a = this; var i, r, b; for (i in fil) { b = "xxx"; for (r=0; r<fil[i].length; r++) { a = a.split(fil[i]).join(b); } } t = a.indexOf("x"); t == -1 ? trace("no hits") : trace("results !!"); return a; }; word = ["badguy", "bastard", "goofy"]; x = "goofy was in town an met the badgy gringo. he wa s a real bastard"; trace(x.changer(word)); Posted by: andi haas | website http://www.andihaas.de |
//nifty dynamic hexadecimal color brightness algorithm, values range from -100% (black) to 100% (white), just like setting the brightness properties of a movieClip function brightness(color_str, ratio) { var hex; for (i=0; i<3; i++) { //loop to slice the rgb values hex = parseInt(color_str.slice(i*2, i*2+2), 16); //slicing the rgb values and parse to decimal values ratio>0 ? hex += Math.ceil((255-hex)/100*ratio) : hex=Math.ceil(hex/100*(100+ratio)); //calculation pending on minus or plus if (hex<16) {// add a zero for single digit values color_str += "0"; } color_str += hex.toString(16); // parse to hexadecimal } return (color_str.slice(6)); //return the new value } trace(brightness("0080ff", -100));//fade to 100% black returns '000000' trace(brightness("0080ff", 100)); //fade to 100% white returns 'ffffff' Posted by: Folko Langner | website http://www.advance-media.com |

